怎么用python导入SQL

如题所述

第1个回答  推荐于2016-07-10
访问MYSQL的话使用,MySQLdb模块
import os,sys
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd="",db='YOURDB')
except Exception,e:
print e
sys.exit()
cursor=conn.cursor()
sql="insert into address(name,address) values (%s,%s)"
values=(("zhang","being"),("li","beijing"),("wang","beijing"))
try:
cursor.executemany(sql,values)#插入数条数据
except Exception , e:
print e
sql="select * from address"
cursor.execute(sql) #查询
data=cursor.fetchall()
if data:
for x in data:
print x[0],x[1]
cursor.close()#关闭游标
conn.close()#关闭数据库本回答被提问者采纳
相似回答