在我的 SQLite 数据库中,我有一个名为 client_dir 的表,它是客户联系方式的目录。我还有一个名为 contracts 的表,其中包含不同合同的详细信息。我需要能够创建将合同条目绑定到 client_dir 中的客户端条目的外键。
问题是,我希望能够通过提供客户名称来生成外键。如何从字段条目中调用行 ID?
我认为它应该看起来像这样......
def new_contract():
'''creates a new contract entry and links it to a client in the client_dir table'''
client = input("Company name...")
#int(row_id) = client
contract = input("New contract...")
self.cursor.execute("INSERT INTO contracts VALUES (?)",contract,#row_id)
更新
我的表如下所示:
self.cursor.execute("""CREATE TABLE client_dir(cli_id INTEGER PRIMARY KEY AUTOINCREMENT, company TEXT, address TEXT, phone INTEGER, email TEXT)""")
self.cursor.execute("""CREATE TABLE contracts(con_id INTEGER PRIMARY KEY AUTOINCREMENT, contract TEXT, client_id INTEGER, FOREIGN KEY(client_id) REFERENCES client_dir(cli_id))""")
我的新 _contract 方法如下所示:
def new_contract(self):
'''creates a new contract entry and links it to a client in the client_dir table'''
client = input("Company name...")
for f_list in self.cursor.execute("SELECT cli_id FROM client_dir WHERE company = (?)",[client]):
f_key = f_list[0]
print(f_key) # For debugging
contract = input("New contract...")
self.cursor.execute("INSERT INTO contracts VALUES (NULL,?,?)",([contract],[f_key]))
print(f_key) 正在屏幕上打印一个漂亮整洁的 1,我认为它是一个 int。但我收到此错误:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
c.new_contract()
File "C:\Python33\smalltime\Client.py", line 63, in new_contract
self.cursor.execute("INSERT INTO contracts VALUES (NULL,?,?)",([contract],[f_key]))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.