pymysql -转自https://www.cnblogs.com/chenhaiming/p/9883349.html#undefined
PyMysql的几个重要方法
- connect函数:连接数据库,根据连接的数据库类型不同,该函数的参数也不相同。connect函数返回Connection对象。
- cursor方法:获取操作数据库的Cursor对象。cursor方法属于Connection对象。
- execute方法:用于执行SQL语句,该方法属于Cursor对象。
- commit方法:在修改数据库后,需要调用该方法提交对数据库的修改,commit方法属于Cursor对象。
- rollback方法:如果修改数据库失败,一般需要调用该方法进行数据库回滚操作,也就是将数据库恢复成修改之前的样子
from pymysql import *
import json def connectDB():
db = connect('127.0.0.1','root','password','databasename')
return db
db = connectDB() def creatTable(db):
cursor = db.cursor()
sql = '''
CREATE TABLE Persons
(
id INT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
age INT NOT NULL,
address CHAR(50),
salary REAL
);
'''
try:
cursor.execute(sql)
db.commit()
return True
except:
db.rollback()
return False def insertRecords(db):
cursor = db.cursor()
try:
cursor.execute('DELETE FROM persons')
cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
VALUES(1,'Paul',32,'California',2000.00)");
cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
VALUES(2,'Allen',25,'Texas',3000.00)");
cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
VALUES(3,'Teddy',23,'Norway',2500.00)");
cursor.execute("INSERT INTO persons(id,name,age,address,salary)\
VALUES(4,'Mark',19,'Rich',5000.00)");
db.commit()
return True
except Exception as e:
print(e)
db.rollback()
return False def selectRecords(db):
cursor = db.cursor()
sql = 'SELECT name,age,address,salary FROM Persons ORDER BY age DESC'
cursor.execute(sql)
results = cursor.fetchall()
print(results)
fields = ['name','age','address','salary']
records = []
for row in results:
records.append(dict(zip(fields,row)))
return json.dumps(records) if creatTable(db):
print('成功创建Persons表')
else:
print('persons表已经存在') if insertRecords(db):
print('成功插入数据')
else:
print('插入记录失败') print(selectRecords(db))
db.close()
pymysql -转自https://www.cnblogs.com/chenhaiming/p/9883349.html#undefined的更多相关文章
- 访问路径:https://i.cnblogs.com/posts?categoryid=925678
https://i.cnblogs.com/posts?categoryid=925678
- URL https://i.cnblogs.com/EditPosts.aspx?opt=1
URL url = new URL("https://i.cnblogs.com");URL url1 = new URL(url, "EditPosts.aspx?op ...
- 随笔二-https://www.cnblogs.com/shang1680/p/9657994.html
作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...
- 211806189杨昊辰 https://www.cnblogs.com/honey1433223/
211806189杨昊辰 https://www.cnblogs.com/honey1433223/
- https://www.cnblogs.com/h2zZhou/p/5440271.html
https://www.cnblogs.com/h2zZhou/p/5440271.html
- https://www.cnblogs.com/soundcode/p/4174410.html
https://www.cnblogs.com/soundcode/p/4174410.html 1.首先要在服务器端新建一个网站axpx页 然后再网站的后台写代码获取winform传过来的文件名. ...
- https://www.cnblogs.com/yudanqu/p/9467803.html
https://www.cnblogs.com/yudanqu/p/9467803.html
- 转发自:一像素 十大经典排序算法(动图演示)原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删
原链接:https://www.cnblogs.com/onepixel/articles/7674659.html 个人收藏所用 侵删 0.算法概述 0.1 算法分类 十种常见排序算法可 ...
- @无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 通篇引用
无痕客 https://www.cnblogs.com/wuhenke/archive/2012/12/24/2830530.html 关于Async与Await的FAQ 关于Async与Await的 ...
随机推荐
- wps excel
ET.Application etApp;ET.workbook etbook;ET.Worksheet etsheet ;ET.Range etrange;//获取工作表表格etApp = new ...
- 【分布式锁】redis实现
转载:https://www.jianshu.com/p/c970cc710SETNX命令简介 SETNX key value 将key的值设为value,并且仅当key不存在. 若给定的key已经存 ...
- defer、return、返回值,这三者的执行逻辑
defer.return.返回值,这三者的执行逻辑是: return 最先执行,return 负责将结果写入返回值中:接着defer执行,可能修改返回值:最后函数携带当前返回值退出.
- 西门子S7-200SMART PLC视频教程(百度网盘)
西门子S7-200SMART PLC视频教程(百度网盘)西门子S7-200 SMART PLC是西门子公司推出的高性价比小型plc,是国内广泛使用的S7-200PLC的更新换代产品. 以下是关于S7- ...
- 【DevExpress】GridView的RowClick事件禁用Checkbox选择的解决办法
添加GridView的RowCellClick事件,代码如下 private void gvBoxMails_RowCellClick(object sender, DevExpress.XtraGr ...
- MQTT研究之EMQ:【基础研究】
EMQ版本V2, emqttd-centos7-v2.3.11-1.el7.centos.x86_64.rpm 下载地址:http://emqtt.com/downloads/2318/centos7 ...
- C#中将鼠标光标变为忙碌状态
this.Cursor = System.Windows.Forms.Cursors.WaitCursor; do_longtime_work(); this.Cursor = System.Wind ...
- 利用rman duplicate重建oracle dataguard standby数据库
问题背景 适用情况: 操作系统: redhat 6.5 数据库: oracle 11g r2 问题描述: failover后原主库无法恢复和启动或者丢失主备关系 优点 不需要对primary数据库停机 ...
- 怎么安装Scrapy框架以及安装时出现的一系列错误(win7 64位 python3 pycharm)
因为要学习爬虫,就打算安装Scrapy框架,以下是我安装该模块的步骤,适合于刚入门的小白: 一.打开pycharm,依次点击File---->setting---->Project---- ...
- docker环境安装与开启远程访问
一,安装docker 1,服务器安装 docker yum install docker 直接yum安装版本太低 2,卸载:老版本的Docker在yum中名称为docker或docker-engine ...