Question

I'm having some bad time with pymysql and python operators. I don't know much about python operator though.

I want to insert into the database some values. The code:

import pymysql
conn = pymysql.connect(host='127.0.0.1', unix_socket='/Applications/MAMP/tmp/mysql/mysql.sock', user='root', passwd='root', db='db2', charset='utf8')
cur = conn.cursor()
v = (123 , 'foobarfoobar', 'foo', 1241, 3, 132 )
cur.execute("INSERT INTO celebs(num_id, Text, Handle, Followers, RT, Timestamp) VALUES (?,?,?,?,?,?)", v)

For the record mySQL structure is INT (PRI), INT, TEXT, VARCHAR, INT, INT, VARCHAR.

I run this and I get TypeError: not all arguments converted during string formatting

Any details how to solve it and possibly some explanation how operator work in python. In php is easier I think: $a = 22; echo "$a days";

Update:

I'm using directly the Insert into and it still doesn't insert the data:

cur.execute("INSERT INTO celebs (num_id, Text, Handle, Followers, RT, Timestamp) VALUES (123 , 'foobarfoobar', 'foo', 1241, 3, 132 );"  )
Was it helpful?

Solution

According to the source code, the paramstyle is format, so you'll need to change...

cur.execute("INSERT INTO ... VALUES (?,?,?,?,?,?)", v)

...to...

cur.execute("INSERT INTO ... VALUES (%s,%s,%s,%s,%s,%s)", v)

If you're using a transactional storage engine such as InnoDB, you'll have to explicitly commit the transaction by calling conn.commit() after doing the INSERT query.

OTHER TIPS

i think the mistake is in the data structure of v . it should be list not tuple

Check mysql engine. Pymysql is not working with InnDB but it is working fine when I changed from InnoDB to MyISAM

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top