0

我在 Sqlite 数据库中有两个表。第一个表的列是_idasprimary key和as 。第二个表的列为, 。note date_cidforeign key_cidprimary keyCategory

一些默认记录,我在创建它时已插入到第二个表中。

两个表都创建成功。但是当我将记录插入第一个表时,没有插入. 没有得到任何类型的异常。请指导我。

Table 1

private static final String DB__CREATE1 = "CREATE TABLE " + TABLE_NAME_CATE + "(_cid INTEGER PRIMARY KEY AUTOINCREMENT, "+ "Category TEXT not null);";

Table 2

private static final String DB__CREATE2 = "CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "+ "Note TEXT not null, "+ "NoteDateTime TEXT not null, " + "_cid INTEGER not null, " + "FOREIGN KEY(_cid) REFERENCES Categories(_cid));";

插入数据的方法

public void insertData1( long c_id, String discription, String nDate){

    ContentValues  contentValues=new ContentValues();
    contentValues.put("note", discription);
    contentValues.put("date", nDate);
    contentValues.put("_cid", c_id);
    open();
    database.insert("TABLE_NAME", null,contentValues);
    close();
}`

调用语句

dbConnector.insertData1(1, textview.getText().toString(), nDateTime.toString());

4

2 回答 2

0

对于第一个表,它应该如下所示:

    private static final String DB__CREATE1 = "CREATE TABLE " + TABLE_NAME_CATE + "( "+_cid+ "INTEGER PRIMARY KEY AUTOINCREMENT, "+Category +"TEXT not null);";

但是如果没有看到完整的代码,很难真正了解它。您是否为“类别”声明了变量(是否为字符串)?

并且对于您的第二个相同,您是否为“_id”、“Note”、“NoteDateTime”等声明了变量?

编辑

好的,我看到了一些我不明白的东西。如上所述,第一个表由 id、注释日期和外键组成。但这应该是这样的:

    private static final String DB__CREATE1 = "CREATE TABLE " + TABLE_NAME_CATE + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, "+ "date TEXT not null,"+ " Category TEXT not null);";

您将 _cid 而不是 _id 传递给第一列,而您忘记了日期列。

然后,在您的第二个表中,您引用“Categories(_cid)”,但它不应该像在您的第一个表中声明的那样是“Category(_cid)”吗?

于 2013-02-27T14:07:30.703 回答
0

如果没有完整的代码,真的很难指出确切的问题。我可以看到之前发布的答案很合乎逻辑。但是,根据我使用多表数据库的经验,事情非常繁琐,并且不像我们仅使用单个表那样工作。

I had a similar problem, I once created a multi-table database and I still don't way what's the problem because it created the tables, but it could not insert. Later I followed this procedure: https://stackoverflow.com/a/5899110/1351867 it worked!

So my guess is, you might have problem with database adapter or helper classes, and maybe it's confusing between two tables. Please post your full code if you still face problems.

于 2013-03-07T06:35:28.907 回答