mysqldb insertion error while inserting Error 1241: Operand should contain 1 column(s)

207 Views Asked by At

dbPickle = pickle.dumps(self.rawRequestObject)

is my pickle which i need to store in a mysql db along with some other data.

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()
# ----------------------------------------------------------------------------------------------------------------------------------------

The above is my method to insert the data into the db.

The below are the values that are being passed to the above method respectively :

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

The data types for each of the above as reported by type() is :

<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

Below is the table schema where I am trying to insert the above values :

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)

And the error I am getting while insertion of the data is :

Error 1241: Operand should contain 1 column(s)

I am using python MySqlDb. Can someone please guide me as to what exactly am I missing ?

I did go through SO questions regarding the same error, but none seem to be applicable for my case.

Found the problem and the solution : After some minute debugging, I realized that the only way to store these in the TEXT columns of MySql would be to first str() them and then also use conn.escape_string(), because it's the ' and " in the values being inserted that's actually causing the problem. So now I am able to insert the values.

However, this brings in another problem. Now when I read back these values from the db, I don't want the escaped ones. I want the ' and " to be there like they were before escaping them. Looks like there is no functions to reverse this and my only way out would be to manually handle it. Is there a better way to do this ?

0

There are 0 best solutions below