Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用外键创建表_Python_Sqlite_Python 2.7 - Fatal编程技术网

Python 使用外键创建表

Python 使用外键创建表,python,sqlite,python-2.7,Python,Sqlite,Python 2.7,我编写了一个函数来创建表: def createSQLCreateTableCommand(tableName, columns, foreignKeys): columns = ["%s"%(c) for c in columns] foreignKeys = ["FOREIGN KEY (%s) REFERENCES %s(%s)"%(fk[0],fk[1],fk[2]) for fk in foreignKeys] cmd = """CREATE TABLE IF

我编写了一个函数来创建表:

def createSQLCreateTableCommand(tableName, columns, foreignKeys):
    columns = ["%s"%(c) for c in columns]
    foreignKeys = ["FOREIGN KEY (%s) REFERENCES %s(%s)"%(fk[0],fk[1],fk[2]) for fk in foreignKeys]
    cmd = """CREATE TABLE IF NOT EXISTS %s (%s)"""%(tableName,','.join(columns+foreignKeys))
    return cmd
我想测试我的函数并编写以下代码。visit和test只是带有一些列的表。在表test_result中,它包含访问表中rowid的外键和测试表中rowid的外键。我的代码在visit和testtable中运行良好,但未能演示测试结果表。我不确定是否正确表达外键参数。有人能帮我修改代码吗

conn = sqlite.connect(":memory:")
cursor = conn.cursor()

sqlVisits = createSQLCreateTableCommand("visit",["visitid text"],[])
sqlTests = createSQLCreateTableCommand("test",["name text","unit text","low text","high text"],[])
sqlTestResults = createSQLCreateTableCommand("test_result",["value text","time_date text"],["visit int","test int"])    #this is not correct

cursor.execute(sqlVisits)
cursor.execute(sqlTests)
cursor.execute(sqlTestResults)
conn.commit()

cursor.execute("""SELECT tbl_name FROM sqlite_master WHERE type = 'table'""")
print cursor.fetchall()

打印函数的实际输出时:

>>> createSQLCreateTableCommand("test_result",["value text","time_date text"],["visit int","test int"])
CREATE TABLE IF NOT EXISTS test_result (
    value text,
    time_date text,
    FOREIGN KEY (v) REFERENCES i(s),
    FOREIGN KEY (t) REFERENCES e(s)
)
您可以看到,像fk[0]这样的表达式从字符串中提取单个字符

参数foreignKeys必须是由以下三个字符串组成的数组:子列名、父表名和父列名:

>>> print createSQLCreateTableCommand("test_result",
...                                   ["value text", "time_date text"],
...                                   [("visitid", "visit", "visitid"),
...                                    ("testname", "test", "name")])
CREATE TABLE IF NOT EXISTS test_result (
    value text,
    time_date text,
    FOREIGN KEY (visitid) REFERENCES visit(visitid),
    FOREIGN KEY (testname) REFERENCES test(name)
)
但是,这不是有效的SQL,因为外键约束必须引用子表中的现有列

要解决此问题,请更改createSQLCreateTableCommand函数以创建具有外键列约束的列:

>>> def createSQLCreateTableCommand(tableName, columns, foreignKeys):
...     columns = ["%s"%(c) for c in columns]
...     foreignKeys = ["%s REFERENCES %s(%s)"%(fk[0],fk[1],fk[2]) for fk in foreignKeys]
...     cmd = """CREATE TABLE IF NOT EXISTS %s (%s)"""%(tableName,','.join(columns+foreignKeys))
...     return cmd
...
>>> print createSQLCreateTableCommand("test_result",
...                                   ["value text", "time_date text"],
...                                   [("visitid text", "visit", "visitid"),
...                                    ("testname text", "test", "name")])
CREATE TABLE IF NOT EXISTS test_result (
    value text,
    time_date text,
    visitid text REFERENCES visit(visitid),
    testname text REFERENCES test(name)
)