插入命令

时间:2016-03-27 10:00:58

标签: java android database sqlite

  

03-27 10:39:55.279 23303-23303 / com.example.danyalahmed.stockmanagement   E / SQLiteLog:(1)外键不匹配 - “产品”引用   “类别”03-27 10:39:55.280   23303-23303 / com.example.danyalahmed.stockmanagement E / SQLiteDatabase:   插入代码时出错= 2536 Price = 5 CategoryID = 1 Quantity = 2 Name =Olá                                                                                            android.database.sqlite.SQLiteException:外键不匹配 -   “Product”引用“Category”(代码1):,同时编译:INSERT   INTO PRODUCT(代码,价格,类别ID,数量,名称)价值(?,?,?,?,?)                                                                                                在   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)                                                                                                在   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895)                                                                                                在   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506)                                                                                                在   android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                                在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                                在   android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                                                                                                在   android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)                                                                                                在   android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)                                                                                                在   com.example.danyalahmed.stockmanagement.Classes.DbAdapter.insertData(DbAdapter.java:131)                                                                                                在   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)                                                                                                在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                在   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);
}

感谢任何帮助。

感谢。

1 个答案:

答案 0 :(得分:0)

我的水晶球告诉我你没有看过documentation而忘记将ID表的Category列作为主键。