2014-06-20 24 views
1

我想爲單個值(行)做一個批量插入,我試圖使用executemany函數來這樣做,但它不會工作,它會返回TypeError: not all arguments converted during string formatting。然而,當我添加一個額外的價值它的工作Python的MySQL數據庫executemany不工作在一個值

所以...... 它會在這裏返回一個錯誤:

entries_list=[("/helloworld"), 
       ("/dfadfadsfdas")] 
cursor.executemany('''INSERT INTO fb_pages(fb_page) 
     VALUES (%s)''', entries_list) 

但不是在這裏:

entries_list=[("/helloworld", 'test'), 
       ("/dfadfadsfdas", 'rers')] 
cursor.executemany('''INSERT INTO fb_pages(fb_page, page_name) 
     VALUES (%s, %s)''', entries_list) 

回答

2

("/helloworld")是一樣的如同寫作"/helloworld"。要創建一個元組,你需要("/helloworld",)

你在做什麼實際運行以下命令:

cursor.executemany('''INSERT INTO fb_pages(fb_page) 
    VALUES (%s)''', ["/helloworld","/dfadfadsfdas"]) 

現在你收到的錯誤是非常合情合理的 - 你只有一個佔位符提供兩個參數。如下定義entries_list將解決問題:

entries_list=[("/helloworld",), 
      ("/dfadfadsfdas",)] 
相關問題