1

I have a list called rowdata = [u'android', u'TSC', u'airtime-ctg', u'guest', u'guest', u'8354e5f4-e18d-11e6-9a46-0242ac110002', u'77c7600c502a3d15'] and want to enter this data in my table ctg_payload.

I have tried :

sqlquery = 'insert into ctg_payload (ID,logtime,hostype,hostype,group_id,group_id,deviceid) values (%s,%s,%s,%s,%s,%s,%s) ; '%(rowdata)

cursor.executemany(sqlquery)

2 Answers 2

2

You cannot do that using % but you can do that using format and argument unpacking:

sqlquery = 'insert into ctg_payload (ID,logtime,hostype,hostype,group_id,group_id,deviceid) values ({},{},{},{},{},{},{}) ; '.format(*rowdata)

simple example which demonstrates the capability:

>>> "{},{}".format(*[1,2])
'1,2'

and you probably mean execute, not executemany which runs execution multiple times, not what you want

Sign up to request clarification or add additional context in comments.

1 Comment

tried this solution but gives me an error: executemany() takes exactly 3 arguments (2 given)
1

As Jean-François Fabre mentioned, *rowdata should be used in str format for your query when you execute a single query!.

And also,

You have to use execute() if you want to execute one query!

This method executes the given database operation (query or command). The parameters found in the tuple or dictionary params are bound to the variables in the operation. Specify variables using %s or %(name)s parameter style (that is, using format or pyformat style). execute() returns an iterator if multi is True.

data=[[u'android', u'TSC', u'airtime-ctg', u'guest', u'guest', u'8354e5f4-e18d-11e6-9a46-0242ac110002', u'77c7600c502a3d15']]

for rowdata in data:
    sqlquery = 'insert into ctg_payload (ID,logtime,hostype,hostype,group_id,group_id,deviceid) values ({},{},{},{},{},{},{}) ; '.format(*rowdata)
    cursor.execute(sqlquery)

And executemany() when

This method prepares a database operation (query or command) and executes it against all parameter sequences or mappings found in the sequence seq_of_params.

data=[[u'android', u'TSC', u'airtime-ctg', u'guest', u'guest', u'8354e5f4-e18d-11e6-9a46-0242ac110002', u'77c7600c502a3d15']]
sqlquery = 'insert into ctg_payload (ID,logtime,hostype,hostype,group_id,group_id,deviceid) values (%s,%s,%s,%s,%s,%s,%s) '
cursor.executemany(sqlquery,data)

6 Comments

i tried using both sqlquery = 'insert into ctg_payload1 (ID,logtime,hostype,hostype,group_id,uuid,deviceid) values (%s,%s,%s,%s,%s,%s,%s) ;'.format(*rowdata) cursor.execute(sqlquery)
sqlquery = 'insert into ctg_payload1 (ID,logtime,hostype,hostype,group_id,uuid,deviceid) values (%s,%s,%s,%s,%s,%s,%s) ;' cursor.executemany(sqlquery,rowdata)
what is the eror that you get? and its not ` cursor.executemany(sqlquery,rowdata)` but cursor.executemany(sqlquery,data)
its the same, u named it data and in my case its rowdata
Content of rowdata is a list or list of lists? I meant list as data and list of lists and rowdata.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.