Python连接MySQL

Python下载URL:http://www.python.org/download/releases

MySQL-Python下载URL:http://www.codegood.com/downloads 

连接的关键是安装MySQLdb模块,这个的版本要与Python的版本相对应。

下载好安装时他会自动检测到计算机的Python安装路径,因此会安装到制定的路径下。

Python安装在:C:/Python26下

MySQLdb自动安装到:C:\Python26\Lib\site-packages\MySQLdb下。 

连接上之后的使用方式:

 1 import MySQLdb 
 2 
 3 conn = MySQLdb.connect(host='localhost',
 4 
 5 user='root',passwd='password',db='dbname') 
 6 
 7 curs = conn.cursor() 
 8 
 9 curs.execute("create database aaa"); 
10 
11 curs.execute("use aaa"); 
12 
13 curs.execute("create table aaa(id int,name char(8))"); 
14 
15 res = curs.fetchall() 
16 
17 print res 
18 
19 curs.close() 
20 
21 conn.close()

 

posted @ 2013-12-21 18:04  Novopo  阅读(390)  评论(0)    收藏  举报