擅长:python、mysql、java
<p>首先,您拥有的<code>db</code>不是一个定义的变量,因此您的代码不应该完全运行。</p>
<blockquote>
<p>\list on this server is a bunch of databases full of usernames, of which my username is one</p>
</blockquote>
<p>下面是你应该如何连接。对于一个数据库,而不是一个表,通常的模式是输入数据库名,然后输入user/pass。</p>
<p><sub>模式是关系数据库中的一个松散术语。表和数据库都有模式,但您似乎希望连接到表,而不是数据库。</sub></p>
<p>因此,请尝试以下代码,尝试修复缩进和SQL注入问题--<a href="http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries" rel="nofollow noreferrer">See this documentation</a></p>
<p>注意,首先必须在要连接的数据库中创建了这两个表。</p>
<pre><code>import json
from psycopg2 import connect
username = 'jungal01'
conn = connect(dbname=username, user=username)
cur = conn.cursor()
with open('gened.json') as gens:
geneds = json.load(gens)
for g in geneds:
cur.execute('''insert into course (number, description, title)
values(%(number)s, %(description)s, %(title)s);''', g)
cur.execute('''insert into requirement (fulfills)
values(%(fulfills)s);''', g)
conn.commit()
</code></pre>