使用Mysql数据库

!/usr/bin/python
-*- coding: UTF-8 -*-

import MySQLdb

#【打开数据库连接】
db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB" )

#【使用cursor()方法获取操作游标】 
cursor = db.cursor()

#【使用execute方法执行SQL语句】
#【查数据,python交互环境不会显示数据,只显示数据总数】
cursor.execute("sqlString")

#【使用 fetchone() 方法获取一条数据库。】
#【显示数据,python交互环境显示具体数据】
data = cursor.fetchone()
print "Database version : %s " % data

#【关闭游标】
cursor.close()

#【关闭数据库连接】
db.close()

# 【多条数据操作】
cursor.executemany("sqlstring",[(),(),()])

fetchmany()

# 【移动光标】
cursor.scroll()

你可能感兴趣的:(使用Mysql数据库)