0

Using old example:

 csr = con.cursor()
 csr.execute('Truncate table test.data')
 csr.executemany('INSERT INTO test.data VALUES (?,?,?,?)', Newdata)
 con.commit()

How would I insert %s into

csr.executemany('INSERT INTO test.data VALUES (?,?,?,?)', Newdata)

tried:

csr.executemany('INSERT INTO test.%s VALUES (?,?,?,?)', Newdata) % symbol

1 Answer 1

4

Should be:

csr.executemany('INSERT INTO test.%s VALUES (?,?,?,?)' % symbol, Newdata)

Although I would instead do something like:

query = 'INSERT INTO test.%s VALUES (?,?,?,?)' % symbol
csr.executemany(query, Newdata)

Note that ? is not the default placeholder on all database wrappers. In some %s is used instead (mysqldb, psycopg2 for instance). In these cases, It's sometimes simpler to build the query in parts.

Doing:

query = 'INSERT INTO test.%s' % symbol
query += ' VALUES (%s,%s,%s,%s)'

otherwise you would need something like:

query = 'INSERT INTO test.%s VALUES (%%s,%%s,%%s,%%s)' % symbol

which is a pain.

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

Comments

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.