[英]C# with SQLite and foreign key
I want to implement a patients data base for our software, and have an issue with the foreign key statement. 我想为我们的软件实现一个患者数据库,并且外键声明有问题。 I am using the latest SQLite DLLs, with C#.
我正在使用带有C#的最新SQLite DLL。
When I try to run below code: 当我尝试运行以下代码时:
dbConnection = "Data Source=SQLiteMyDatabase.db;foreign keys=true;";
if (connections == 0)
{
cnn = new SQLiteConnection(dbConnection);
cnn.Open();
this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS patients ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL;");
this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS images ( FOREIGN KEY (patientID) REFERENCES patients(id), nameRed VARCHAR(20) NOT NULL PRIMARY KEY;");
}
I get the error: 我收到错误:
SQLite error near "FOREIGN": syntax error
Why does this happen? 为什么会这样?
In order to create a foreign key, you must first create the column: 为了创建外键,必须首先创建列:
CREATE TABLE IF NOT EXISTS images (
nameRed VARCHAR(20) NOT NULL PRIMARY KEY,
patientID INT,
FOREIGN KEY (patientID) REFERENCES patients(id)
);
Please note: 请注意:
nameRed
) first in the list of columns created, but that's just convention. nameRed
),但这只是约定。 It could go second.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.