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()