CREATE DATABASE `python_test` CHARSET UTF8
import MySQLdb
con = MySQLdb.connect(host="localhost", user="root", passwd="******",db="python_test",port=3306)
cur = con.cursor()
cur.execute('create table stu_info (name char(128) not null default "", age tinyint(3) not null default 0, sex enum("man","femal") not null default "man") engine=innodb charset=utf8')
#0L
con.commit()
cur.execute("insert into stu_info (name, age, sex) values ('Yi_Zhi_Yu',25,'man')")
con.commit()
cur.execute("insert into stu_info (name, age, sex) values (%s,%s,%s)", ("Tony",25, "man"))
con.commit()
cur.executemany("insert into stu_info (name, age, sex) values (%s,%s,%s)",(("LiMei",26,"femal"),("YuanYuan",28,"femal")))
con.commit()
cur.execute("select * from stu_info")
stus = cur.fetchall()
(('Yi_Zhi_Yu', 25, 'man'),
('Tony', 25, 'man'),
('LiMei', 26, 'femal'),
('YuanYuan', 28, 'femal'))
for stu in stus:
print "name: %s; age: %d; sex: %s" %(stu[0], stu[1], stu[2])
name: Yi_Zhi_Yu; age: 25; sex: man name: Tony; age: 25; sex: man name: LiMei; age: 26; sex: femal name: YuanYuan; age: 28; sex: femal
cur = con.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cur.execute("select * from stu_info")
cur.fetchall()
({'age': 25, 'name': 'Yi_Zhi_Yu', 'sex': 'man'},
{'age': 25, 'name': 'Tony', 'sex': 'man'},
{'age': 26, 'name': 'LiMei', 'sex': 'femal'},
{'age': 28, 'name': 'YuanYuan', 'sex': 'femal'})
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有