| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
pythopen
9年前发布

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

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

#!/usr/bin/python     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()