python3.x连接mysql

 1 import mysql.connector;#连接数据库
 2 
 3 mdb=mysql.connector.connect(host='172.20.20.3',user='root',passwd='zhubajie',db='zhubajie_market',port=3306);
 4 # 使用cursor()方法获取操作游标 
 5 yb=mdb.cursor();
 6 sql="SELECT manager_id,manager_name,group_id from mk_allot_user limit 10";
 7 try:
 8     #执行语句
 9     yb.execute(sql);
10     #获取所有记录
11     results = yb.fetchall();
12     for row in results:
13         manager_id = row[0];
14         manager_name = row[1];
15         group_id = row[2];
16         print(manager_id,'|',manager_name,'|',group_id);
17     
18 except mysql.connector.Error as e:
19     print("Error: unable to fecth data");
20 #关闭游标
21 yb.close();
22 mdb.close();

 

posted @ 2016-06-17 14:06  YuDiGeGe  阅读(159)  评论(0编辑  收藏  举报