Python 培训之MySql
1. Install
1.1 install mysql
sudo apt-get install lamp-server^ (tip: Set password)
1.2 install MySQLdb
1 sudo apt-get install python-dev 2 sudo apt-get install libmysqlclient-dev 3 sudo apt-get install python-pip 4 sudo pip install MySQL-python
2. Commands
2.1 View
1 mysql -u root -ppasswd 2 show databases; 3 use database; 4 show tables; 5 describe table; 6 select * from table where colname = "##"; 7 (tip:count(*),limit;orderby;distinct,like)
2.2 Use
create database ###; CREATE TABLE chem_gene ( chem_gene_id int NOT NULL AUTO_INCREMENT, chemicalName varchar(255) NOT NULL, genesymbol varchar(100) NOT NULL, interaction text NOT NULL, interactionActions text NOT NULL, CONSTRAINT chem_gene PRIMARY KEY(chem_gene_id) ) ; insert into tablename values(##,###,###)
2.3. update && delete
update ### set colname1=### where colname2=###; delete from tablename; delete from tablename where colname = ###; drop table tablename; drop database databasename;
2.4. coding
create table (character set = utf8;)
alter table ### convert to character set utf8;
2.5 create web user
mysql -u root -ppasswd grant insert ,create,delete,select,drop,alter,index,update on dbname.* to username identified by "passwd"; mysql -u username -ppasswd
3. Mysql Work with python
3.1 sql script create database && create table
CREATE TABLE chem_gene ( chem_gene_id int NOT NULL AUTO_INCREMENT, chemicalName varchar(255) NOT NULL, chemicalID varchar(100) NOT NULL, casId varchar(100) NOT NULL, genesymbol varchar(100) NOT NULL, geneid varchar(100) NOT NULL, geneforms varchar(255) NOT NULL, organism varchar(100) NOT NULL, organismid varchar(100) NOT NULL, interaction text NOT NULL, interactionActions text NOT NULL, pubmedid varchar(100) NOT NULL, CONSTRAINT chem_gene PRIMARY KEY(chem_gene_id) ) character set = utf8;
(tip: mysql -u username -ppasswd -D dbname < ####.sql )
3.2. python insert by MySQLdb
#!/usr/bin/env/python # -*- coding:UTF-8 -*- from __future__ import print_function import sys import MySQLdb fin = sys.argv[1] table = sys.argv[2] def mysql_conn(ip,user,passwd,db,port): try: conn = MySQLdb.connect(host=ip,user=user,
passwd=passwd,db=db,port=port,charset='utf8') cur = conn.cursor() return conn,cur except MySQLdb.Error,e: print(e.args) sys.exit(1) def insert_db(conn,cursor,command): try: cursor.execute(command) conn.commit() except MySQLdb.Error,e: print(e.args) sys.exit(1) with open(fin) as input: conn,cur = mysql_conn("192.168.1.104","tcd_net","tcd_net","ctd_net",3308) for line in input: if not line.startswith("#"): content = line.strip().split("\t") out = tuple([""]+content) command = "insert into %s values%s;"%(table,out) print(command) try: insert_db(conn,cur,command) except: print("insert failed") sys.exit(1) conn.close() cur.close()
(tip: python ###.py ###.txt tablename )
3.3 python query by MySQLdb
#!/usr/bin/env/python # -*- coding:UTF-8 -*- from __future__ import print_function import MySQLdb import sys chem_name = sys.argv[1] def mysql_connect(): try: conn = MySQLdb.connect(host="localhost",user="tcd_net", passwd='tcd_net',db='ctd_net',port=3306,charset='utf8') cur = conn.cursor() return conn,cur except MySQLdb.Error,e: print(e.args) sys.exit(1) def query_db(conn,cursor,command): try: cursor.execute(command) conn.commit() results = cursor.fetchall() return results except MySQLdb.Error,e: print(e.args) sys.exit(1) def chem_gene_query(chem): conn,cur = mysql_connect() query_command = """select chemicalName,chemicalID,casId, genesymbol,geneid,geneforms,organism,organismid,interaction, interactionActions,pubmedid from chem_gene where chemicalName = "%s";"""%chem chem_gene_result = query_db(conn,cur,query_command) for res in chem_gene_result: print("\t".join(res)) chem_gene_query(chem_name)
(tip: python ###.py chem_name)
4. Mysql import && dump
mysql -u root -ppasswd -D dbname < ****.sql
mysqldump -u root -ppasswd dbname > ****.sql
5..Mysql config
sudo service mysql status/start/restart/stop /etc/mysql/my.cnf [client] port = 3308 socket = /var/run/mysqld/mysqld.sock [mysqld] user = mysql port = 3308 datadir = /var/lib/mysql bind-address = 0.0.0.0