<p>要访问<code>list of dictionaries</code>的数据,您需要使用一个循环来访问,请使用它的双列表</p>
<p>我用<code>sqlite3</code>创建了一个数据库,这样你就可以了解我在做什么</p>
<p><strong>首先,我创建了数据库:</strong></p>
<pre class="lang-py prettyprint-override"><code>__author__ = "{D/R}"
import sqlite3
conn = sqlite3.connect("demo.sqlite", detect_types=sqlite3.PARSE_DECLTYPES)
conn.execute("CREATE TABLE IF NOT EXISTS dicListTable ("
"name TEXT NOT NULL,"
"age INTEGER NOT NULL)")
</code></pre>
<p><strong>词典列表:</strong></p>
<pre><code>list_dic = [{'name': 'sandeep', 'age': 10}, {'name': 'sa', 'age': 14}]
</code></pre>
<p><strong>功能:</strong></p>
<pre class="lang-py prettyprint-override"><code>def func(list_of_dict):
cursor = conn.cursor()
query = "insert into dicListTable (name, age) VALUES (?, ?)"
for i in range(0, len(list_of_dict)):
cursor.executemany(query, ((list_of_dict[i]["name"], list_of_dict[i]["age"]),))
cursor.connection.commit()
</code></pre>
<p>调用函数</em></p>
<pre><code>func(list_dic)
</code></pre>
<p><em>关闭数据库</em></p>
<p><code>conn.close()</code></p>
<p><strong>输出</strong></p>
<pre><code>+ -+ -+
| name | age |
+ -+ -+
| sandeep | 10 |
+ -+ -+
| sa | 14 |
+ -+ -+
</code></pre>