如何利用python3创建数据表_python3创建表及表数据;
創(chuàng)建表,參考代碼如下;
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()
向表中添加數(shù)據(jù);
方式一、
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()
方式二、#從外部傳入?yún)?shù)
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()
方式三、錯(cuò)誤回滾
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('執(zhí)行錯(cuò)誤,并且已回滾')
test.close()
總結(jié)
以上是生活随笔為你收集整理的如何利用python3创建数据表_python3创建表及表数据;的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python mulit函数_pytho
- 下一篇: 软件维护费 一般按照多少比例_2021年