def _select(sql, num, *args):
"""
执行SQL,返回一个结果 或者多个结果组成的列表
"""

try:

if num > 100:

print "num can't big than 100"

return

engine =mysql.connector.connect(user=user, password=password, database=database, host=host, port=port)

cursor = engine.cursor()
cursor.execute(sql, num,args)
d = cursor.fetchall()
vaules = d[0:num]
if cursor.description:
names = [x[0] for x in cursor.description]

return [Dict(names, x) for x in vaules]

finally:
if cursor:

cursor.close()

定义了一个方法_select(),本意想封装一个方法,方便对mysql的方便操作:返回num条查询结果,但是发觉如果没有对查询结果全部取回的话,会报错:Unread result found,所以如果换成如下的代码就会抛出异常:


try:

if num > 100:

print "num can't big than 100"

return

engine =mysql.connector.connect(user=user, password=password, database=database, host=host, port=port)

cursor = engine.cursor()
cursor.execute(sql, num,args)
#d = cursor.fetchall()
#vaules = d[0:num]
if cursor.description:
names = [x[0] for x in cursor.description]

return [Dict(names, x) for x in cursor.fetchmany(num)]

finally:
if cursor:

cursor.close()




更多相关文章

  1. [置顶] sqlserver性能调优方法论与常用工具
  2. ORACLE-SQL:定义并使用多字符串变量
  3. 外部主机不允许连接Mysql设置的解决方法
  4. 关于SQL2005安装完毕后,没有SQL Server Management Studio问题的
  5. MySQL忘记密码破解密码的方法
  6. win8安装sql server2005方法
  7. mysql关键字与表名字段相同的解决方法
  8. MySQL延迟关联性能优化方法
  9. MySQL各存储引擎的区别及其启动方法

随机推荐

  1. Android: Android Wifi System
  2. android WebView loadData不能解析
  3. 本博博文预报
  4. 如何去写 Android(安卓)init.rc (Android
  5. 开启 Android 反射调用
  6. Android MediaController
  7. 通过JS或PHP检测Android
  8. Android在设置里面添加新功能的方法
  9. 关于android:focusable属性
  10. [AndroidTips]Android预定义样式