0

dbPickle = pickle.dumps(self.rawRequestObject)

是我的泡菜,我需要将其与其他一些数据一起存储在 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(),因为它是 ' 和" 在插入的值中,这实际上是导致问题的原因。所以现在我可以插入这些值了。

然而,这带来了另一个问题。现在,当我从数据库中读回这些值时,我不想要转义的值。我希望 ' 和 " 像在逃避它们之前一样存在。看起来没有功能可以扭转这种情况,我唯一的出路是手动处理它。有没有更好的方法来做到这一点?

4

0 回答 0