Home  >  Q&A  >  body text

How to change the default single quotes in the executemany method in Python's MySQLdb library?

I need to create hundreds of tables. The table names are: abc_1, abc_2, abc_3...
I used execute to create them before, but I thought it was a bit slow, so now I thought about whether I could use executemany, and then I found that executemany would use single parameters for the parameters. enclosed in quotation marks,

>>cur.execute('''create table %s (id int(10));''',('abc_1',))
create table 'abc_1' ( #单引号抛出异常

>>cur.execute('''create table `%s` (id int(10));''',('abc_1',))
create table `'abc_'` ( #创建成功,但创建的表名多出两个单引号

But our library does not allow table names or field names to be enclosed in single quotes, resulting in a direct error.

滿天的星座滿天的星座2687 days ago630

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-18 10:50:40

    Try removing the symbols before and after %s in your statement create table %s (id int(10));.

    reply
    0
  • Cancelreply