Python sql操作1

import pymysql
conn=pymysql.connect("localhost",port=3306,user="root",passwd="xsy7437",db="studentdb",charset="utf8")
cursor=conn.cursor()
data=[
("李四","20","2017-09-12","男"),
("王五","22","2017-10-15","男"),
("赵玲","23","2017-11-12","女"),
]
sql_insert="insert into student (姓名,年龄,register_date,sex) values (%s,%s,%s,%s)"
sql_select="select * from student"
try:
# 执行SQL语句
cursor.executemany(sql_insert,data)
conn.commit()
cursor.execute(sql_select )
# 获取所有记录列表
results = cursor.fetchall()
for row in results:
id = row[0]
name= row[1]
age = row[2]
sex = row[4]
register_date = row[3]
# 打印结果
print( "ID=%d,姓名=%s,年龄=%s,性别=%s,注册日期=%s" %(id, name, age, sex, register_date ))
except:
print("操作不成功.......")
conn.close()
posted @ 2017-11-05 21:07  沧海一粒水  阅读(123)  评论(0)    收藏  举报