Py学习  »  DATABASE

第四章 数据储存——JSON、CSV、Excel、MySQL(十三) 2020-01-14

但丁的学习笔记 • 4 年前 • 288 次点击  

十二、 MySQL 数据库– 数据库插入操作

插入数据


sql = “insert into user(id,username,gender,age,password) values(null,’abc’,1,18,’111111’)”

cursor.execute(sql)

db.commit()

db.close()

如果在数据还不能保证的情况下,可以使用以下方式来插入数据:

sql = “insert into user(id,username,gender,age,password) values(null,%s,%s,%s,%s)”

cursor.execute(sql,(‘spider’,1,20,’222222’))



示例代码


import pymysql

db =pymysql.connect(host="127.0.0.1", port=3306, user="root",password="root", database="csdn_crawler")

curso r= db.cursor()

# sql = "insert into article(id, title, content) values(null, '222','333')"

# cursor.execute(sql)

title = '444'

content = '555'

sql = "insert into article(id, title, content) values(null, %s, %s)"

# 动态插入

cursor.execute(sql,(title, content))

db.commit()

db.close()



上一篇文章 第四章 数据储存——JSON、CSV、Excel、MySQL(十二) 2020-01-13 地址:

https://www.jianshu.com/p/1ee154302c2d



以上资料内容来源网络,仅供学习交流,侵删请私信我,谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52935
 
288 次点击