title: PyMySQL插入
author: 杨晓东
permalink: PyMySQL插入
date: 2021-10-02 11:27:04
categories:
- 投篮
tags:
- demo
PyMySQL MySQL数据库_插入
#!/usr/bin/python3
import pymysql
ip = "localhost"
user = 'root'
pwd = '1234'
# 打开数据库连接
db = pymysql.connect(ip, user, pwd, "matt")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# SQL 插入语句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
print('ok')
except:
# 如果发生错误则回滚
db.rollback()
print('on NO')
# 关闭数据库连接
db.close()
"""
或者 :
# SQL 插入语句
sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \
LAST_NAME, AGE, SEX, INCOME) \
VALUES ('%s', '%s', %s, '%s', %s)" % \
('Mac', 'Mohan', 20, 'M', 2000)
try:
# 执行sql语句
cursor.execute(sql)
# 执行sql语句
db.commit()
except:
# 发生错误时回滚
db.rollback()
# 关闭数据库连接
db.close()
"""
标签:NAME,db,PyMySQL,cursor,插入,sql
From: https://www.cnblogs.com/demo443/p/17154228.html