2019-02-28
阅读量:
1441
如何在python中使用多行sql语句
问题描述:
工作中经常要用五六十行甚至更长的SQL语句进行查询,大概就是这样
select * from user
where uid < 10000
and age < 30;
现在要用查出来的数据在python中进行数据分析,请问如何能把已经写好的多行SQL语句直接复制到python中执行?
解决方法:
import MySQLdb
sql = "select * from user" +
"where uid < 10000" +
"and age < 30"
# 打开数据库连接
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
cursor = db.cursor()
# 使用execute方法执行你的SQL语句
cursor.execute(sql)
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print "Database version : %s " % data
# 关闭数据库连接
db.close()






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论