今天比較忙,沒時間學編程,學了一個資料庫插入操作:
import MySQLdb
# 打開資料庫連接
db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' )
# 使用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()
except:
# Rollback in case there is any error
db.rollback()
# 關閉資料庫連接
db.close()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/224350.html
標籤:其他
上一篇:case運算式---多條件運算式
下一篇:pymysql常用方法總結
