First, installation
Install the compiled version number (this method is quick and easy):
Http://www.codegood.com/downloads
Download according to your own system, double-click Install, fix
Then import MySQLdb. See if it's successful
My, win7,32 bit, 2.7 version number
Mysql-python-1.2.3.win-amd32-py2.7.exe
Second, use
#!/usr/bin/python
# Encoding:utf-8
Import Time,mysqldb
# Open Database connection
db = MySQLdb.connect ("localhost", "root", "root", "Python")
# get an action cursor using the cursor () method
cursor = Db.cursor ()
#删除表
sql = "DROP table if exists Thinkgamer"
Cursor.execute (SQL)
#创建
sql = "CREATE table if not exists Thinkgamer (name varchar (+) Primary key,created int (10))"
Cursor.execute (SQL)
#写入
sql = "INSERT into Thinkgamer (name,created) VALUES (%s,%s)"
param = ("AAA", Int (Time.time ()))
n = cursor.execute (Sql,param)
print ' Insert ', n
#写入多行
sql = "INSERT into Thinkgamer (name,created) VALUES (%s,%s)"
param = (("BBB", Int (Time.time ())), ("CCC", +), ("ddd", 44))
n = Cursor.executemany (Sql,param)
Print "Insertmany", n
#更新
sql= "Update Thinkgamer set name=%s where name= ' aaa '"
param = ("zzz")
n = cursor.execute (Sql,param)
Print "Updata", n
#查询
n = Cursor.execute ("SELECT * from Thinkgamer")
For row in Cursor.fetchall ():
Print row
For R in row:
Print R
#删除
sql = "Delete from Thinkgamer where name =%s"
param = ("BBB")
n = cursor.execute (Sql,param)
Print "Delete", n
#查询
n = Cursor.execute ("SELECT * from Thinkgamer")
Print Cursor.fetchall ()
Cursor.close ()
#提交
Db.commit ()
#关闭
Db.close ()
Output Result:
Insert 1
Insertmany 3
Updata 1
(' zzz ', 1436067892L)
zzz
1436067892
(' BBB ', 1436067892L)
Bbb
1436067892
(' CCC ', 33L)
Ccc
33
(' DDD ', 44L)
Ddd
44
Delete 1
(' zzz ', 1436067892L), (' CCC ', 33L), (' DDD ', 44L))
A lot of other details please poke: mysqldb User ' s Guide
Installation and use of MYSQLDB