python3mysql获取

2024-06-28 12:18:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码:

import sys
import mysql.connector
import hashlib

#empty dict of hash :  snapshot
rowHash = dict()

#connect to destdb
dest2cnx = mysql.connector.connect(user='user', password='password', host='localhost', database='destDB')
#get cursor for dest
destCur = destcnx.cursor()

#list of src datebase
srcdblist = ['src1','src2','src3','src4','src5','src6','src7','src8','src9','src10','src11','src12','src13','src14','src15','src16','src17','src18']


#Count combined row evaluated
count = 0

for srcdb in srcdblist:
    #display to console  DB access
    print("Accessing DB: " + str(srcdb))

    #connect to src db
    srccnx = mysql.connector.connect(user='user2', password='password',host='otherloc',database=srcdb)
    srcCUR = srccnx.cursor()


    sql="SELECT * FROM `src_tbl` WHERE 1 ;"

    srcCUR.execute(sql)

    row = srcCUR.fetchone()

    while row:
        #hash
        hashVal = hashlib.md5(str(row).encode('utf-8')).hexdigest()

        #check if in rowHash Dictionary
        if hashVal in rowHash.keys():
            # if there already
            rowHash[hashVal] = rowHash[hashVal] + srcdb + '~src_tbl|'
        else:
            #failing that
            snapshotAdd =  srcdb + '~src_tbl|'
            #add to dict
            rowHash[hashVal] = snapshotAdd
            #insert into purplebox2 database set up SQL
            sql = "INSERT INTO `dest_tbl` (`item_id`,`comments`,`accessDate`,`restock`,`meta_hash`) VALUES "
            insertValues = str(row)[:-1] + ", '"+hashVal+"' )"
            sql = sql + insertValues + " ;"
            #Execute SQL
            destCur.execute(sql)

        #Count loc
        count += 1

        if (count % 500) == 0 :
            print ("viewing total :"+ str(count) + " in db "+srcdb)

        #load next row
        row = srcCUR.fetchone()

    destcnx.commit() # commit changes after each db
    srcCUR.close
    srccnx.close

print("Total count: "+str(count)+", flushing snapshots to DB")

#flush snapshots to db
flushcount = 0
for (hashtxt, snapShot) in rowHash.items():
    sql = "UPDATE `dest_tbl` SET `meta_snapshot` = '"+snapShot+"' WHERE `dest_tbl`.`meta_hash` = '"+hashtxt+"';"
    destCur.execute(sql)
    flushcount += 1
    if (count % 250) == 0:
        print("flushing : "+str(count)+" into DB")

destcnx.commit()
destCur.close()
destcnx.close()

在src6到src9中运行了几次之后,它崩溃了,给出了以下错误,我对代码所做的所有更改都清除了db用户的详细信息。 任何帮助都将不胜感激。在

错误:

^{pr2}$

Tags: toinsrcdbsqlifcounthash