鱼C论坛

 找回密码
 立即注册
查看: 6538|回复: 3

python连接mysql出错的问题求助!

[复制链接]
发表于 2016-12-4 00:31:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位鱼友,我写了一段将文件分拣并入库的代码,代码如下:

windows 7 64位 + python 3.3.2 + mysql 5.5.53,使用mysql .connector连接。

import os
import mysql.connector
#连接数据库
config = {
    'user':'root',
    'password':'macau853',
    'host':'192.168.100.205',
    'database':'xl',
    'raise_on_warnings':True
    }
cnx = mysql.connector.connect(**config)
cursor=cnx.cursor()
#计算行数
def linenumber(path,filename):
    count=0  
    fp=open(path+filename,"r")  
    while 1:
        buffer=fp.read(8*1024*1024)  
        if not buffer:
            break
        count+=buffer.count('\n')
    fp.close()
    return(count)

#第一步先将原始的话单分拣到要求的格式,并入库
sqlallbill = "insert into allbill(time,id,number,one,two)values(%s,%s,%s,%s,%s)"
filename = os.listdir(r'Z:\download')
count = 0
for i in filename:
    allbillrecord = ()
    allbilldata = []
    bill = open(r'Z:\download\\' + i)
    line = bill.readlines()   
    l = len(line)
    if l > 5000:
        for i in range(1,l):
            line2 = line[i][160:174].strip() + ',' + line[i][:30].strip() + ',' + line[i][46:51].strip() + ',' + line[i][69:80].strip() + ',' + line[i][56:69].strip()
            allbillrecord = line2.split(sep = ',')
            allbilldata.append(allbillrecord)
            if count == 5000:
                cursor.executemany(sqlallbill, allbilldata)
                cnx.commit()               
            count += 1
        if count > 0:
            cursor.executemany(sqlallbill, allbilldata)
            cnx.commit()
    else:
        for i in range(1,l):
            line2 = line[i][160:174].strip() + ',' + line[i][:30].strip() + ',' + line[i][46:51].strip() + ',' + line[i][69:80].strip() + ',' + line[i][56:69].strip()
            allbillrecord = line2.split(sep = ',')
            allbilldata.append(allbillrecord)
    del line
    del allbillrecord
    del allbilldata
    bill.close()
cnx.close()

然后运行
>>>
Traceback (most recent call last):
  File "C:\Python33\lib\site-packages\mysql\connector\network.py", line 241, in recv_plain
    chunk = self.sock.recv(4 - packet_len)
ConnectionAbortedError: [WinError 10053] 您的主机中的软件中止了一个已建立的连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Z:\insertmysql.py", line 45, in <module>
    cursor.executemany(sqlallbill, allbilldata)
  File "C:\Python33\lib\site-packages\mysql\connector\cursor.py", line 654, in executemany
    return self.execute(stmt)
  File "C:\Python33\lib\site-packages\mysql\connector\cursor.py", line 551, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 490, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Python33\lib\site-packages\mysql\connector\connection.py", line 267, in _send_cmd
    return self._socket.recv()
  File "C:\Python33\lib\site-packages\mysql\connector\network.py", line 270, in recv_plain
    errno=2055, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at '192.168.100.205:3306', system error: 10053 您的主机中的软件中止了一个已建立的连接。

有朋友遇到过这个问题吗?是什么导致的,多谢。如有违规,请版主知会我、
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-12-4 00:33:21 | 显示全部楼层
BTW,在报错之前,数据库已经成功插入115135数据了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-4 02:02:27 | 显示全部楼层
问题找到了:

for i in range(1,l):
            line2 = line[i][160:174].strip() + ',' + line[i][:30].strip() + ',' + line[i][46:51].strip() + ',' + line[i][69:80].strip() + ',' + line[i][56:69].strip()
            allbillrecord = line2.split(sep = ',')
            allbilldata.append(allbillrecord)
            if count == 5000:---------------------------------------------由于count在第一次到达5000后,没有重置其值为0,导致除了第一次到达5000执行插入语句,就再也无法触发了,导致长时间连接空置,故数据库断开连接。
                cursor.executemany(sqlallbill, allbilldata)
                cnx.commit()               
            count += 1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-12-4 02:03:55 | 显示全部楼层
代码修改如下:

for i in range(1,l):
            line2 = line[i][160:174].strip() + ',' + line[i][:30].strip() + ',' + line[i][46:51].strip() + ',' + line[i][69:80].strip() + ',' + line[i][56:69].strip()
            allbillrecord = line2.split(sep = ',')
            allbilldata.append(allbillrecord)
            if count == 5000:
                cursor.executemany(sqlallbill, allbilldata)
                cnx.commit()   
                count = 0 ----------------------------重置一下count值即可解决,Oh yeah!
            count += 1
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-11 03:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表