在centos6.4系统上使用Python编写连接数据库需要使用的rpm
MySQL-python-1.2.3-0.3.c1.1.el6.x86_64
python连接、操作数据库实例脚本如下所示:
mysql.py
#!/usr/bin/python
#coding=utf-8
import MySQLdb
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456')
curs=conn.cursor()
curs.execute("drop database if exists pythondb")
curs.execute("create database pythondb")
conn.select_db('pythondb')
curs.execute("create table test(id int,message varchar(50))")
value=[1,"davehe"]
curs.execute("insert into test values(%s,%s)",value)
values=[]
for i in range(20):
values.append((i,'hello mysqldb' + str(i)))
for i,j in list(values):
print i,j
curs.executemany("insert into test values(%s,%s)",values)
conn.commit()
curs.close()
conn.close()