【发布时间】:2013-12-02 09:08:54
【问题描述】:
我正在编写使用 mysql 数据库的 python 脚本。我想保护 mysql 连接,并且本地用户无法访问数据库。有什么好的方法来生产这个吗?
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","username","password","db")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
cursor.execute("CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, userid VARCHAR(30),
activity_id VARCHAR(30), date_time VARCHAR(30), screenshot_filename VARCHAR(255), screenshot_md5 VARCHAR(255), num_clicks INT, num_of_mouse_movements INT, num_pause INT );")
try:
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
【问题讨论】: