引入数据库的包
import MySQLdb
连接数据库
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test',port=3303,charset='utf8')
#创建游标
cur=conn.cursor()
sql1="insert into user (title,name,content) values (%s,%s,%s)"
#cur.executemany("insert into user (title,name,content) values (%s,%s,%s)",[('test05','z05','test content05'),('test06','zz06','test content06'),])
#cur.execute("insert into user (title,name,content) values ('test05','z05','test content05'),('test06','zz06','test content06')")
#conn.commit()
#返回有影响的行数
print cur.execute("select * from user")
#返回全部
print cur.fetchall()
print cur.fetchall()
#返回size条
print cur.fetchmany(size)
#返回一条
print cur.fetchone()
#从当前行移动到value
cur.scroll(value,mode='relative')
#从结果集移到value行
cur.scroll(value,mode='absolute')
踩过的坑:
连接的时候端口号不能加引号,不然会报错:TypeError: an integer is required
execute 和executemany 用法区别:
cur.execute("insert into user (title,name,content) values ('test05','z05','test content05'),('test06','zz06','test content06')")
cur.executemany("insert into user (title,name,content) values (%s,%s,%s)",[('test05','z05','test content05'),('test06','zz06','test content06'),])