Python Sqlite3: INSERT INTO table VALUE(dictionary goes here)

后端 未结 9 587
野趣味
野趣味 2020-12-24 12:26

I would like to use a dictionary to insert values into a table, how would I do this?

import sqlite3

db = sqlite3.connect(\'local.db\')
cur = db.cursor()

c         


        
9条回答
  •  无人及你
    2020-12-24 12:46

    key_lst = ('status', 'title', 'chapters', 'onchapter', 'genre', 'type')
    cur.execute('INSERT INTO Media (status,title,chapters,onchapter,genre,type) VALUES ' + 
                '(?,?,?,?,?,?);)',tuple(values[k] for k in key_lst))
    

    Do your escaping right.

    You probably also need a commit call in there someplace.

提交回复
热议问题