本文链接:https://blog.csdn.net/Mr__lqy/article/details/85719603

1. 连接mysql

import pymysql
db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
cursor = db.cursor()
sql = 'select * from students;'
cursor.execute(sql)
cursor.close()
db.close()

2. 多字段动态插入mysql数据库中

import pymysql
db = pymysql.connect(host='localhost',user='root', password='', port=3306, db='spiders')
data = {
'id': '',
'name': 'Lily',
'age': 20
}
table = 'students'
keys = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
try:
cursor.execute(sql, tuple(data.values()))
print('Successful')
db.commit()
except:
print('Failed')
db.rollback()
cursor.close()
db.close()

3. 数据更新插入mysql数据库中

import pymysql
db = pymysql.connect(host='localhost',user='root', password='', port=3306, db='spiders')
data = {
'id': '',
'name': 'Lily',
'age': 25
}
table = 'students'
keys = ', '.join(data.keys())
values = ', '.join(['%s'] * len(data))
sql = 'INSERT INTO {table}({keys}) VALUES ({values}) ON DUPLICATE KEY UPDATE'.format(table=table, keys=keys, values=values)
update = ','.join([" {key} = %s".format(key=key) for key in data])
sql += update
try:
cursor.execute(sql, tuple(data.values())*2)
print('Successful')
db.commit()
except:
print('Failed')
db.rollback()
cursor.close()
db.close()

python往mysql数据库中写入数据和更新插入数据的更多相关文章

  1. PHP往mysql数据库中写入中文失败

    该类问题解决办法就是 在建立数据库连接之后,将该连接的编码方式改为中文. 代码如下: $linkID=@mysql_connect("localhost","root&q ...

  2. 通过java代码往mysql数据库中写入日期相关数据少13个小时

    通过show variables like '%time_zone%'; 查看时区: CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard ...

  3. python连接mysql数据库,并进行添加、查找数据

    1.删除MySQL数据表中的记录 DELETE FROM table_name WHERE condition; python操作mysql1数据库 import pymysql # 连接mysql数 ...

  4. MySQL 数据库中如何把A表的数据插入到B表?

    web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码 ...

  5. python 往mysql数据库中插入多条记录。

    最近想写mysql库,用到insert into语句,如何一次性将多条记录插入库表中呢. MySQLdb提供了两个执行语句的方法:一个是execute(),另一个是executemany() exec ...

  6. Python连接MySQL数据库中各种坑

    第一个坑 要想连接数据库,我们必须拥有MySQL-python这个模块,首先,我在安装这个模块的时候就到了第一个大坑. 常规安装方法:进入cmd 使用 pip install MySQL-python ...

  7. python 将mysql数据库中的int类型修改为NULL 报1366错误,解决办法

    gt.run_sql()是用pymysql 封装的类 distribution_sort_id type: int目的:将此字段值全部修改为NULL g=2gt.run_sql("updat ...

  8. 使用Python比较MySQL数据库中两个数据库的表结构--转载

    https://blog.csdn.net/tenaguan4461/article/details/82286781 https://www.jianshu.com/p/b3dac5a3479a

  9. 从mysql数据库中查询最新的一条数据的方法

    第一种方法 SELECT * from a where id = (SELECT max(id) FROM a); 第二种方法: select * FROM 表名 ORDER BY id DESC L ...

随机推荐

  1. 干货 | MySQL数据库安全之审计

    干货 | MySQL数据库安全之审计 原创: 李勇 京东云开发者社区  今天 每家公司都希望业务高速增长,最好能出几个爆款产品或者爆款业务,从而带动公司营收高速攀升.但站在数据库管理员的角度,这却是实 ...

  2. Unix网络编程学习 < 一 >

    #include "unp.h" int main(int argc , char**argv) { int sockfd , n; //sockfd套接字描述符 ]; struc ...

  3. PAT A1009-1012

    A 1009 Product of Polynomials (25 point(s)) 读懂题意就行. #include <cstdio> #include <iostream> ...

  4. String 字符串拼接

    字符串拼接有两个方法 第一中 var sad = "happy" var variable = "you"+sad +"" variable ...

  5. css伪元素::before与::after使用基础示例

    1.指定文本前后添加内容 <div class="box">test</div> .box::before{ content: 'before'; marg ...

  6. mysql6数据库安装与配置

    详细步骤可以参考这篇文章 https://www.cnblogs.com/duguangming/p/10623520.html 1.下载并打开mysql6数据库安装文件 2.默认点击下一步 3.点击 ...

  7. HDU 2544 最短路 【Dijkstra模板题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...

  8. Is the MIME type 'image/jpg' the same as 'image/jpeg'?

    https://stackoverflow.com/questions/33692835/is-the-mime-type-image-jpg-the-same-as-image-jpeg No, i ...

  9. How to .gitignore all files/folder in a folder, but not the folder itself?

    https://stackoverflow.com/questions/4250063/how-to-gitignore-all-files-folder-in-a-folder-but-not-th ...

  10. -bash: fultter: command not found

    flutter build apk bash: flutter: command not found 在studio中的控制台出现上面错误(如图所示) 解决办法: 安装flutter时,安装时可以执行 ...