python如何控制数据库?

时间:2023-03-09 03:50:24
python如何控制数据库?

http://www.w3cschool.cc/python/python-mysql.html

通过利用MySQLdb可以操作数据库

实例:

以下实例链接Mysql的TESTDB数据库:

# encoding: utf-8
#!/usr/bin/python import MySQLdb # 打开数据库连接
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # 使用cursor()方法获取操作游标
cursor = db.cursor() # 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()") # 使用 fetchone() 方法获取一条数据库。
data = cursor.fetchone() print "Database version : %s " % data # 关闭数据库连接
db.close()