首页 > 编程语言 > 详细

python3创建表及表数据;

时间:2019-12-26 01:25:47      阅读:80      评论:0      收藏:0      [点我收藏+]

创建表,参考代码如下;

import pymysql
test=pymysql.connect(‘localhost‘,‘root‘,‘root‘,‘test1225‘)
curs=test.cursor()
curs.execute(‘drop table if exists xixi‘)
sql="""
create table `xixi`(`names` varchar(255) default null,
`age` int(3) default null,
`income` decimal(8,2) default 0)
ENGINE=InnoDB DEFAULT charset=utf8;
"""
curs.execute(sql)
test.close()

技术分享图片

 

 

 

 

 

 

 向表中添加数据;

方式一、

import pymysql
test=pymysql.connect(‘localhost‘,‘root‘,‘root‘,‘test1225‘)
curs=test.cursor()
sql="""
insert into `xixi` values(‘xiaohua‘,34,888.3)
"""
curs.execute(sql)
test.commit()
test.close()
方式二、#从外部传入参数
import pymysql
test=pymysql.connect(‘localhost‘,‘root‘,‘root‘,‘test1225‘)
curs=test.cursor()
names=‘张的美‘
age=18
curs.execute(‘insert into xixi(names,age) values("%s",%d)‘%(names,age))
test.commit()
test.close()
方式三、错误回滚
import pymysql
test=pymysql.connect(‘localhost‘,‘root‘,‘root‘,‘test1225‘)
curs=test.cursor()
names=‘张的美‘
age=18
money=888
try:
curs.execute(‘insert into xixi values("%s",%d,%f)‘ % (names, age, money))
test.commit()
except:
test.rollback()
print(‘执行错误,并且已回滚‘)
test.close()



python3创建表及表数据;

原文:https://www.cnblogs.com/canglongdao/p/12099454.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!