Python:如何将字典插入sqlite数据库?

时间:2021-05-25 18:21:03

I have a sqlite database with a table with following columns :

我有一个sqlite数据库,其中包含以下列的表:

id(int) , name(text) , dob(text)

I want to insert following dictionary to it :

我想在其中插入以下字典:

{"id":"100","name":"xyz","dob":"12/12/12"}

Dictionary keys are the column names. How can i achieve it ?

字典键是列名。我怎样才能实现它?

4 个解决方案

#1


6  

Looking at the documentation here you can add a single row:

查看此处的文档,您可以添加一行:

c.execute("INSERT INTO stocks VALUES (?,?,?)", [dict["id"], dict["name"], dict["dob"]])

Or you can use a list and add multiple rows in one go:

或者您可以使用列表并一次添加多行:

# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

#2


3  

If, for example, c = conn.cursor(), and your dictionary is named dict and your table tablename, then you can write

例如,如果c = conn.cursor(),并且你的字典被命名为dict和你的表tablename,那么你可以写

c.execute('insert into tablename values (?,?,?)', [dict['id'], dict['name'], dict['dob']])

Which will insert the elements of the dictionary into the table as you require.

这将根据您的需要将字典的元素插入表中。

#3


3  

Here's a way which preserves parameter safety. (Might need polishing in the tablename department)

这是一种保持参数安全的方法。 (可能需要在tablename部门进行抛光)

def post_row(conn, tablename, rec):
    keys = ','.join(rec.keys())
    question_marks = ','.join(list('?'*len(rec)))
    values = tuple(rec.values())
    conn.execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)

row = {"id":"100","name":"xyz","dob":"12/12/12"}
post_row(my_db, 'my_table', row)

#4


1  

As per Gareth‘s response, if you're using MySQLdb you can use executemany and pass a list of values which you can get directly from your dict using dict.values()

根据Gareth的回复,如果您正在使用MySQLdb,您可以使用executemany并传递一个值列表,您可以使用dict.values()直接从您的dict获取这些值。

#1


6  

Looking at the documentation here you can add a single row:

查看此处的文档,您可以添加一行:

c.execute("INSERT INTO stocks VALUES (?,?,?)", [dict["id"], dict["name"], dict["dob"]])

Or you can use a list and add multiple rows in one go:

或者您可以使用列表并一次添加多行:

# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
             ('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
             ('2006-04-06', 'SELL', 'IBM', 500, 53.00),
            ]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)

#2


3  

If, for example, c = conn.cursor(), and your dictionary is named dict and your table tablename, then you can write

例如,如果c = conn.cursor(),并且你的字典被命名为dict和你的表tablename,那么你可以写

c.execute('insert into tablename values (?,?,?)', [dict['id'], dict['name'], dict['dob']])

Which will insert the elements of the dictionary into the table as you require.

这将根据您的需要将字典的元素插入表中。

#3


3  

Here's a way which preserves parameter safety. (Might need polishing in the tablename department)

这是一种保持参数安全的方法。 (可能需要在tablename部门进行抛光)

def post_row(conn, tablename, rec):
    keys = ','.join(rec.keys())
    question_marks = ','.join(list('?'*len(rec)))
    values = tuple(rec.values())
    conn.execute('INSERT INTO '+tablename+' ('+keys+') VALUES ('+question_marks+')', values)

row = {"id":"100","name":"xyz","dob":"12/12/12"}
post_row(my_db, 'my_table', row)

#4


1  

As per Gareth‘s response, if you're using MySQLdb you can use executemany and pass a list of values which you can get directly from your dict using dict.values()

根据Gareth的回复,如果您正在使用MySQLdb,您可以使用executemany并传递一个值列表,您可以使用dict.values()直接从您的dict获取这些值。