python2连接MySQL获取表数据字段数据随机传入列表,随机取值
#!/usr/bin/env python# -*- coding:utf-8 -*-import MySQLdbdef get_cmsdb():# 打开数据库连接db = MySQLdb.connect("172.16.18.3", "test", "test@123", "fsviewdb_HLJ", charset='utf8' )# 使用curso...
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import MySQLdb
def get_cmsdb():
# 打开数据库连接
db = MySQLdb.connect("172.16.18.3", "test", "test@123", "fsviewdb_HLJ", charset='utf8' )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 查询语句
sql = "SELECT id,fuserid FROM userprofile LIMIT 10;"
user_list = []
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
# print results
for row in results:
fuserid = row[0]
print "fuserid=%s"%(fuserid)
# fuserid = row[1]
# 打印结果
user_list.append(str(fuserid))
except:
print "Error: unable to fecth data"
# 关闭数据库连接
db.close()
return user_list
print get_cmsdb()
更多推荐
所有评论(0)