dbPickle = pickle.dumps(self.rawRequestObject)
是我的pickle,我需要存储在mysql数据库中以及其他一些数据。
def addNewResultsToDb(self, reqTimeStamp = None, reqNumber = None, reqPort = None, reqScheme = None, reqMethod = None, reqPath = None, reqHeaders = None, reqQueryParams = None, reqBody = None, putThisPickleInDb = None):
insertValueQry = "INSERT INTO request_logs(request_as_on, request_number, request_port, request_scheme, request_method, request_path, request_headers, request_query_params, request_body, session_pickle) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
# picking out values to be inserted (the different parts of the request), in the db, from the arguments to the method
print "\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
print reqTimeStamp
print reqNumber
print reqPort
print reqScheme
print reqMethod
print reqPath
print reqHeaders
print reqQueryParams
print reqBody
print type(reqTimeStamp)
print type(reqNumber)
print type(reqPort)
print type(reqScheme)
print type(reqMethod)
print type(reqPath)
print type(reqHeaders)
print type(reqQueryParams)
print type(reqBody)
print "\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
try:
debugMessage = "DataAccessObject --> addNewResultsToDb() --> try {} inserting data in table"
debug(debugMessage)
self.cur.execute(insertValueQry,(reqTimeStamp, reqNumber, reqPort, reqScheme, reqMethod, reqPath, reqHeaders, reqQueryParams, reqBody, putThisPickleInDb))
self.con.commit()
except mdb.Error, e:
if self.con:
self.con.rollback()
print "Error %d: %s" % (e.args[0],e.args[1])
exit(1)
finally:
if self.con:
self.con.close()
# ----------------------------------------------------------------------------------------------------------------------------------------
以上是我将数据插入数据库的方法。
以下是分别传递给上述方法的值:
16-09-2015 16:18:01
1
80
http
POST
['3', 'resource', 'android', 'appConfigs']
[['sn', '2.VI7D9DF640615B4948854C88C5E769B94C.SIE5FB3A28D0DA4F27A3D2C03B8FAAFFAE.VS144113550487914225873.1442395729'], ['Browser-Name', 'Mobile Safari'], ['Accept-Encoding', 'gzip'], ['secureToken', '5nANXZrlYBrl0UByhA+qlpLsjHXlnF97tQLHnPgcjwZm9u0t8XZHtO4XTjKODcIb0ee4LlFchmUiptWZEPDUng=='], ['User-Agent', 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; Google Galaxy Nexus - 4.3 - API 18 - 720x1280 Build/JLS36G) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 FKUA/Retail/590206/Android/Mobile (Genymotion/Google Galaxy Nexus - 4.3 - API 18 - 720x1280/fd6babaa1ff9127ed7e9cc7a916639e5)'], ['Device-Id', 'fd6babaa1ff9127ed7e9cc7a916639e5'], ['Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'], ['Host', 'mobileapi.flipkart.net'], ['Connection', 'Keep-Alive'], ['Content-Length', '749']]
[]
{"params":{"appVersion":"3.4.0.5","language":"EN","screenSize":"xhdpi"},"versions":{"blockedSharingApps":"blockedSharingApps.3","appUpgradeData":"appUpgradeData.16","blockedAppVersions":"blockedAppVersions.1","adsDataConfig":"adsDataConfig.36","sellerFilters":"sellerFilters.2","webViewABDataMap":"webViewABDataMap.6","serviceProfileData":"serviceProfileData.2","visualConfig":"visualConfig.45","rules":"rules.3.4.0.5.1","menuItem":"menuItem.3.4.0.5.3","batchNetworkingData":"batchNetworkingData.3","reviewFilters":"reviewFilters.1","jsResources":"jsResources.101","appRateData":"appRateData.6","appTheme":"appTheme.7","imageconfig":"imageconfig.xhdpi.21","actionToUrl":"actionToUrl.10","appendPincode":"appendPincode.2","messages":"messages.EN.2"}}
dbPickle
type()报告的每个数据类型是:
<type 'str'>
<type 'int'>
<type 'int'>
<type 'str'>
<type 'str'>
<type 'list'>
<class 'netlib.odict.ODictCaseless'>
<class 'netlib.odict.ODict'>
<type 'str'>
and the last one being a pickle itself dbPickle
下面是我尝试插入上述值的表架构:
CREATE TABLE IF NOT EXISTS request_logs(request_as_on DATETIME, request_number TEXT, request_port TEXT, request_scheme TEXT, request_method TEXT, request_path TEXT, request_headers TEXT, request_query_params TEXT, request_body TEXT, session_pickle BLOB)
我在插入数据时遇到的错误是:
错误1241:操作数应包含1列
我正在使用python MySqlDb。有人可以指导一下我到底错过了什么吗?
我确实经历过有关同一错误的SO问题,但似乎没有一个适用于我的情况。
发现问题和解决方案: 经过一段时间的调试后,我意识到将这些存储在MySql的TEXT列中的唯一方法是首先使用str()它们然后使用conn.escape_string(),因为它是插入的值中的'和'。实际上导致问题。所以现在我能够插入值。
然而,这带来了另一个问题。现在,当我从数据库中读回这些值时,我不想要转义它们。我希望'和'在逃离它们之前就像它们一样。看起来没有任何功能可以扭转它,我唯一的出路就是手动处理它。有没有更好的方法来做到这一点?