程式語言 - Python - v3.x - Connect MySQL



import os
import sys
import MySQLdb

DB = 'xxx'
USER = 'xxx'
PASSWD = 'xxx'
IP = 'xxx.xxx.xxx.xxx'

db = MySQLdb.connect(IP, USER, PASSWD, DB, charset='utf8')
cur = db.cursor()
cur.execute('lock table mytable write;')
cur.execute('select * from mytable;')
ret = cur.fetchall()
cur.execute('unlock table;')
db.commit()
db.close()