數(shù)據(jù)庫(kù)插入操作在python的數(shù)據(jù)庫(kù)操作
提問(wèn)人:楊紫紅發(fā)布時(shí)間:2020-11-24
代碼如下:
#!/usr/bin/python
# encoding: utf-8
import MySQLdb
# 打開(kāi)數(shù)據(jù)庫(kù)連接
db = MySQLdb.connect("localhost","root","361way","test" )
# 使用cursor()方法獲取操作游標(biāo)
cursor = db.cursor()
# SQL 插入語(yǔ)句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# 執(zhí)行sql語(yǔ)句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫(kù)執(zhí)行
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# 關(guān)閉數(shù)據(jù)庫(kù)連接
db.close()
#!/usr/bin/python
# encoding: utf-8
import MySQLdb
# 打開(kāi)數(shù)據(jù)庫(kù)連接
db = MySQLdb.connect("localhost","root","361way","test" )
# 使用cursor()方法獲取操作游標(biāo)
cursor = db.cursor()
# SQL 插入語(yǔ)句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# 執(zhí)行sql語(yǔ)句
cursor.execute(sql)
# 提交到數(shù)據(jù)庫(kù)執(zhí)行
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# 關(guān)閉數(shù)據(jù)庫(kù)連接
db.close()
繼續(xù)查找其他問(wèn)題的答案?
相關(guān)視頻回答
點(diǎn)擊加載更多評(píng)論>>