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