Question

I can't get ? instead of %s to work on an insert with python mysqldb.

I try

cur.execute("""INSERT INTO project (project) VALUES (?) """, ('ham'))

but I get

query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

works fine if I simply substitute ? with %s

What am I doing wrong?

Was it helpful?

Solution

You can't. The mysqldb adapter only supports the format and pyformat parameter styles.

The Python DB API 2.0 specification supports 5 different paramstyle parameter styles but it is up to the exact database adapter implementation what they support. The mysqldb project documentation tells you what styles are supported by that adapter, and qmark is not one of them.

If you are trying to write SQL suitable for multiple database adapters, you'll most likely have to deal with more issues than just the parameter style. Consider using SQLAlchemy instead, which can generate SQL for you that is more database agnostic (up to a point).

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