python连接MySQL

 1 import sys
 2 import MySQLdb
 3 import json
 4 import time
 5 import string
 6  
 7 cur_time="""+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"""
 8  
 9 with open('../../conf/default.conf','r') as confFile:
10     confStr = confFile.read()
11     conf = json.JSONDecoder().decode(confStr);
12     dbStaticResult = conf['database']['db_bim_dict_db'];
13  
14     conn = MySQLdb.connect(host=dbStaticResult['host'],
15             user=dbStaticResult['user'],
16             passwd=dbStaticResult['password'],
17             db=dbStaticResult['database'],
18             port=dbStaticResult['port']);
19     cur = conn.cursor();
20  
21 file= open(sys.argv[1]);
22 while 1:
23     line= file.readline()
24     if len(line) == 0:
25         break
26     queryaccount=line.split('	')
27     selection= 'select a.name from a join b on a.id=b.id where b.id='+queryaccount[1];
28     cur.execute(selection)
29     accountname = cur.fetchone();
30     print queryaccount[0],queryaccount[1].rstrip(),accountname[0];
31 file.close()
32 conn.commit()
33 conn.close();
原文地址:https://www.cnblogs.com/susidian/p/10012481.html