繁体   English   中英

插入命令上的 SQLite 外键不匹配

[英]SQLite foreign key mismatch on insert Command

03-27 10:39:55.279 23303-23303/com.example.danyalahmed.stockmanagement E/SQLiteLog:(1) 外键不匹配——“产品”引用“类别”03-27 10:39:55.280 23303/com .example.danyalahmed.stockmanagement E/SQLiteDatabase: Error inserting Code=2536 Price=5 CategoryID=1 Quantity=2 Name=Olá android.database.sqlite.SQLiteException: 外键不匹配 - “Product”引用“Category”(代码 1) : , 编译时: android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database 中的INSERT INTO PRODUCT(Code,Price,CategoryID,Quantity,Name) VALUES (?,?,?,?,?)。 sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database .sqlite.SQLiteProgram.(SQLiteProgram.java:58) 在 android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) 在 android.database.sqlite.SQLiteD atabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.example.danyalahmed.stockmanagement.Classes.DbAdapter.insertData(DbAdapter.java:131) at com .example.danyalahmed.stockmanagement.Activities.Scan_Page$3.onClick(Scan_Page.java:162) 在 android.view.View.performClick(View.java:4856) 在 android.view.View$PerformClick.run(View.java: 19956) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:211) 在 android。 app.ActivityThread.main(ActivityThread.java:5373) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

如果你检查这个,

Error inserting Code=2536 Price=5 CategoryID=1 Quantity=2 Name=Olá

你可以看到我的 CategoryID = 1。

当我创建数据库时,我将类别放在那里:

INSERT INTO Category VALUES(1, 'OTHERS');
INSERT INTO Category VALUES(2, 'Crisp');
INSERT INTO Category VALUES(3, 'Sweet');

这是产品结构:

CREATE TABLE IF NOT EXISTS Product (" +
      " _id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE," +
      "Name VARCHAR NOT NULL," +
      "Code VARCHAR NOT NULL," +
      "Quantity INTEGER NOT NULL," +
      "PRICE DOUBLE NOT NULL," +
      "CategoryID INTEGER NOT NULL," +
      "FOREIGN KEY(CategoryID) REFERENCES Category(ID));

这是查询:

public boolean insertData(String Table, String[] Columns, String[] Values) {   
    db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    for (int i = 1; i < Columns.length; i++) {
        contentValues.put(Columns[i].replace(Table + ".", ""), Values[i].trim());
    }
    return ((db.insert(Table, null, contentValues)) != -1);
}

任何帮助表示赞赏。

谢谢。

我的水晶球告诉我,您没有阅读文档,忘记将Category表的ID列设为主键。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM