python操作mysql数据库的简单范例演示

2022-04-24 00:00:00 操作 演示 范例

下面的python代码通过MySQLdb模块链接mysql数据库,然后打开数据库,并通过sql语句查询mysql的版本号,最后关闭数据库连接

import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data
# disconnect from server
db.close()

相关文章