我正在尝试从ftp位置读取图像并将图像中的数据插入到mysql中,同时将图像复制到另一个位置。 但我的问题是它读取图像将数据插入到mysql表中,并且无法将图像复制到不同的位置。
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
def downloadfile(ftpobj, filename, camId):
ftpobj.voidcmd("TYPE I")
datasock, estsize = ftpobj.ntransfercmd("RETR %s" % filename)
transbytes = 0
myFilename = "/tmp/%s" % filename
fd = open(myFilename, 'wb')
#fd = StringIO()
while 1:
buf = datasock.recv(2048)
if not len(buf):
break
fd.write(buf)
transbytes += len(buf)
fd.close()
# Set the file to rw everybody for import
os.chmod(myFilename, 438)
print "Adding %s\n" % filename
tmp = split('_', filename)
tmp2 = split('-', tmp[0])
vrm = tmp2[0]
nationality = tmp2[1]
cameraName = tmp[1]
myDate = tmp[2]
(myYear, myMonth, myDay) = split('-', tmp[2])
tmp2 = split('\.', tmp[3])
(myHour, myMin, mySec, milliSecs) = split('-', tmp2[0])
myTime = "%s:%s:%s" % (myHour, myMin, mySec)
dateTime = "%s %s" % (myDate, myTime)
datetimeObj = datetime.datetime(int(myYear), int(myMonth), int(myDay), int(myHour), int(myMin), int(mySec))
#imageData = fd.getvalue()
tableName = "customer_1.%s_anr_vega" % cameraName
sql = """INSERT INTO %s (camera_id, name, plate, datetime, millisecs, nationality, image_name) values (%s, %s, %s, %s, %s, %s, %s); """ % (tableName,
dbutil.quote(camId, "int"),
dbutil.quote(cameraName, "text"),
dbutil.quote(vrm,"text"),
dbutil.quote(dateTime,"text"),
dbutil.quote(milliSecs,"int"),
dbutil.quote(nationality,"text"),
dbutil.quote(filename, "text"))
cursor.execute(sql)
sql = """SELECT id FROM %s ORDER BY id DESC""" % tableName
cursor.execute(sql)
tmp = cursor.fetchone()
myId = tmp[0]
sql = """UPDATE %s SET IMAGE=LOAD_FILE('%s') WHERE image_name = "%s";""" % (tableName,
myFilename,
filename)
cursor.execute(sql)
# Remove the image file
os.remove(myFilename)
datasock.close()
ftpobj.voidresp()
sys.stdout.write("\n")
[1]: http://i.stack.imgur.com/yVrSR.jpg
[2]: http://i.stack.imgur.com/GC7Q5.jpg
[3]: http://i.stack.imgur.com/hwOpv.jpg