Python 连接mysql

下面我们使用MySQLdb 实现连接mysql 数据库并进行操作. 

#!/usr/bin/env python
# -*-coding:UTF-8-*-
import  MySQLdb
def connetMysql():
conn=MySQLdb.connect(user="admin",passwd="123456",host="XXX",db="test")
cursor = conn.cursor()
cursor.execute("select * from test;")
print "rows:cursor.rowcount() 
for row in cursor.fetchall():
print "note : ", row[0], row[1]
cursor.close() 
if __=='__main__':
connectMysql()

更多资料关注:www.kootest.com ;技术交流群:182526995

原文地址:https://www.cnblogs.com/kootest/p/4088056.html