Sqlite 如何使用外键创建 table
Sqlite how to create table with Foreign key
所以我搜索了一些关于这个主题的问题,但我意识到这些问题与是否允许创建没有 PK 的表等更相关。我的问题是以下代码生成了一个一个错误:E/SQLiteLog: (1) near "FOREIGN": syntax error
代码:
CREATE TABLE tblPart(PartNumber [nvarchar](15) PRIMARY KEY NOT NULL,
PartDescription [nvarchar](150) NOT NULL);
CREATE TABLE tblStock(PartNumber [nvarchar](15) FOREIGN KEY REFERENCES
tblPart(PartNumber) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
StockOH [int] NOT NULL);
在 MSSQLSERVER
中执行此代码完美无缺,但是当在 Java/Android
中使用 SQLite
运行 时,它会生成上述错误。
预先感谢您的帮助。
编辑:
数据处理器Class:
public myDAL(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//region create tblPart
sbCreatePart.append("CREATE TABLE " + TABLE_PART + "(");
sbCreatePart.append(COLUMN_PART_PARTNR + " [nvarchar](15) PRIMARY KEY NOT NULL, ");
sbCreatePart.append(COLUMN_PART_DESCRIPTION + " [nvarchar](150) NOT NULL)");
//endregion
//region create tblCurrentStock
sbCreateCurrStock.append("CREATE TABLE " + TABLE_CURRENTSTOCK + "(");
sbCreateCurrStock.append(COLUMN_CURRENTSTOCK_PARTNR + " [nvarchar](15) FOREIGN KEY REFERENCES "
+ TABLE_PART + "(" + COLUMN_PART_PARTNR + ") ON DELETE CASCADE ON UPDATE CASCADE NOT NULL, ");
sbCreateCurrStock.append(COLUMN_CURRENTSTOCK_STOCKOH + " [int] NOT NULL)");
//endregion
db.execSQL(sbCreatePart.toString());
Log.d("Test", "tblPart");
db.execSQL(sbCreateCurrStock.toString());
Log.d("Test", "tblCurrStock");
}
public boolean TestInsert()
{
boolean result = true;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
try
{
values.put(COLUMN_PART_PARTNR, "PNTest123");
values.put(COLUMN_PART_DESCRIPTION, "Dummy Test Part");
Log.d("Test Input", "Values Added");
db.insert(TABLE_PART, null, values);
Log.d("Test Input", "Insert Successful");
}
catch (Exception ex)
{
result = false;
}
finally
{
db.close();
}
return result;
}
Activity Class:
public void onPartsClick(View v)
{
myDAL dal = new myDAL(this, null, null, 1);
boolean x = dal.TestInsert();
if(x)
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
你有语法错误试试这个:
CREATE TABLE tblPart(PartNumber nvarchar(15) PRIMARY KEY NOT NULL,
PartDescription nvarchar(150) NOT NULL);
CREATE TABLE tblStock(PartNumber nvarchar(15) REFERENCES
tblPart(PartNumber) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
StockOH integer NOT NULL);
如果将外键定义放在列定义上,则不需要 FOREIGN KEY
。
或这样试试:
CREATE TABLE tblStock10(PartNumber nvarchar(15),
StockOH integer NOT NULL,
FOREIGN KEY (PartNumber) REFERENCES tblPart1(PartNumber)
ON UPDATE SET NULL);
所以我搜索了一些关于这个主题的问题,但我意识到这些问题与是否允许创建没有 PK 的表等更相关。我的问题是以下代码生成了一个一个错误:E/SQLiteLog: (1) near "FOREIGN": syntax error
代码:
CREATE TABLE tblPart(PartNumber [nvarchar](15) PRIMARY KEY NOT NULL,
PartDescription [nvarchar](150) NOT NULL);
CREATE TABLE tblStock(PartNumber [nvarchar](15) FOREIGN KEY REFERENCES
tblPart(PartNumber) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
StockOH [int] NOT NULL);
在 MSSQLSERVER
中执行此代码完美无缺,但是当在 Java/Android
中使用 SQLite
运行 时,它会生成上述错误。
预先感谢您的帮助。
编辑:
数据处理器Class:
public myDAL(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//region create tblPart
sbCreatePart.append("CREATE TABLE " + TABLE_PART + "(");
sbCreatePart.append(COLUMN_PART_PARTNR + " [nvarchar](15) PRIMARY KEY NOT NULL, ");
sbCreatePart.append(COLUMN_PART_DESCRIPTION + " [nvarchar](150) NOT NULL)");
//endregion
//region create tblCurrentStock
sbCreateCurrStock.append("CREATE TABLE " + TABLE_CURRENTSTOCK + "(");
sbCreateCurrStock.append(COLUMN_CURRENTSTOCK_PARTNR + " [nvarchar](15) FOREIGN KEY REFERENCES "
+ TABLE_PART + "(" + COLUMN_PART_PARTNR + ") ON DELETE CASCADE ON UPDATE CASCADE NOT NULL, ");
sbCreateCurrStock.append(COLUMN_CURRENTSTOCK_STOCKOH + " [int] NOT NULL)");
//endregion
db.execSQL(sbCreatePart.toString());
Log.d("Test", "tblPart");
db.execSQL(sbCreateCurrStock.toString());
Log.d("Test", "tblCurrStock");
}
public boolean TestInsert()
{
boolean result = true;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
try
{
values.put(COLUMN_PART_PARTNR, "PNTest123");
values.put(COLUMN_PART_DESCRIPTION, "Dummy Test Part");
Log.d("Test Input", "Values Added");
db.insert(TABLE_PART, null, values);
Log.d("Test Input", "Insert Successful");
}
catch (Exception ex)
{
result = false;
}
finally
{
db.close();
}
return result;
}
Activity Class:
public void onPartsClick(View v)
{
myDAL dal = new myDAL(this, null, null, 1);
boolean x = dal.TestInsert();
if(x)
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
你有语法错误试试这个:
CREATE TABLE tblPart(PartNumber nvarchar(15) PRIMARY KEY NOT NULL,
PartDescription nvarchar(150) NOT NULL);
CREATE TABLE tblStock(PartNumber nvarchar(15) REFERENCES
tblPart(PartNumber) ON DELETE CASCADE ON UPDATE CASCADE NOT NULL,
StockOH integer NOT NULL);
如果将外键定义放在列定义上,则不需要 FOREIGN KEY
。
或这样试试:
CREATE TABLE tblStock10(PartNumber nvarchar(15),
StockOH integer NOT NULL,
FOREIGN KEY (PartNumber) REFERENCES tblPart1(PartNumber)
ON UPDATE SET NULL);