Python与MySQL

1、anaconda安装MySQL模块

     cmd -> conda install mysql-python 

     import MySQLdb

2、连接数据库

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 import MySQLdb
 5 
 6 # 打开数据库连接
 7 db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
 8 
 9 # 使用cursor()方法获取操作游标 
10 cursor = db.cursor()
11 
12 # 使用execute()方法执行SQL语句
13 cursor.execute("SELECT VERSION()")
14 
15 # 使用 fetchone()方法获取一条数据库。
16 data = cursor.fetchone()
17 
18 print "Database version : %s " % data
19 
20 # 关闭数据库连接
21 db.close()
原文地址:https://www.cnblogs.com/yml6/p/7262487.html