Pymysql在查询期间丢失了与MySQL服务器的连接

时间:2017-07-17 05:05:11

标签: mysql python-2.7 pymysql

我尝试使用pymsql在服务器上运行一个简单的查询,这不应该是一个问题。我现在差点拉我的头发。我错过了什么。

 import pymysql

 connection = pymysql.connect(user='mak_aba', passwd='password',host='localhost',database='baba')

 cursor = connection.cursor(buffered=True)

 query = ("SELECT * FROM `orders`")

 cursor.execute(query)

 for item in cursor:
   print item  

1 个答案:

答案 0 :(得分:3)

你忘了设置端口了 试试这个

connection = pymysql.connect(host='localhost', port=3306, user='mak_aba', passwd='password', db='baba')

此处example

试试这个

import pymysql

connection = pymysql.connect(host='localhost', port=3306, user='mak_aba', passwd='password', db='baba')

cursor = connection.cursor()

query = ("SELECT * FROM `orders`")

cursor.execute(query)

for item in cursor:
    print(item)