我的代码:
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用户详细信息。 任何帮助将非常感激。
Traceback (most recent call last):
File "C:/Python34/PROJECT/import_dbs.py", line 69, in <module>
row = dkCUR.fetchone()
File "C:\Python34\lib\site-packages\mysql\connector\cursor.py", line 811, in fetchone
row, self.description)
File "C:\Python34\lib\site-packages\mysql\connector\conversion.py", line 403, in row_to_python
result[i] = self._cache_field_types[field_type](row[i], field)
File "C:\Python34\lib\site-packages\mysql\connector\conversion.py", line 430, in _INT_to_python
return int(value)
ValueError: invalid literal for int() with base 10: b'314\x044255\x1821312sSasA1'