python3创建表及表数据;,,创建表,参考代码如下
python3创建表及表数据;,,创建表,参考代码如下
创建表,参考代码如下;
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创建表及表数据;
相关内容
- Python爬虫进阶——JsonPath使用案例,,如果一个网站采用
- Python爬虫知识点——响应,,响应:响应状态码、响
- python面试_总结02,,- 代码题1、创建一
- 【python爬虫】selenium的三种等待,,一、 强制等待 第
- Python高级应用程序设计任务,,Python高级应用
- 利用Python进行数据分析-Pandas(第五部分-数据规整:聚合
- Python-EEG工具库MNE中文教程(10)-信号空间投影SSP数学原理
- python批量执行脚本,使用unittest.TestSuit(); unittest.TextTe
- python基础 import 模块 包,,代码组织结构以及每个
- Python可视化 | Seaborn包—kdeplot和distplot,,import pan
评论关闭