【问题标题】:I'm getting 0 results in this query. What am I doing wrong? (Android)我在这个查询中得到 0 个结果。我究竟做错了什么? (安卓)
【发布时间】:2016-05-16 18:29:15
【问题描述】:

我在 Android 上使用 SQLite。这是我的三张表:

String query = "create table " + TABLE_PERSON + " (" +
            PERSON_COL_ID + "  integer PRIMARY KEY AUTOINCREMENT, " +
            PERSON_COL_NAME + " text, " +
            PERSON_COL_SEX + " text CHECK (" + PERSON_COL_SEX + " IN ('M', 'F'))" +
            ");";
db.execSQL(query);

query = "create table " + TABLE_CONCEPT + " (" +
            CONCEPT_COL_ID + " integer PRIMARY KEY AUTOINCREMENT, " +
            CONCEPT_COL_NAME + " text, " +
            CONCEPT_COL_DATE + " text " +
            ");";
db.execSQL(query);

query = "create table " + TABLE_DEBT + " (" +
            DEBT_COL_ID + " integer PRIMARY KEY AUTOINCREMENT, " +
            DEBT_COL_CONCEPT + " integer REFERENCES " + TABLE_CONCEPT + ", " +
            DEBT_COL_CREDITOR + " integer REFERENCES " + TABLE_PERSON + ", " +
            DEBT_COL_DEBTOR + " integer REFERENCES " + TABLE_PERSON + ", " +
            DEBT_COL_ORIGAMT + " integer, " +
            DEBT_COL_PAIDSOFAR + " integer, " +
            DEBT_COL_DATEREPAID + " text" +
            ");";
db.execSQL(query);

插入3个人,2个概念和1个债务后,我在进行以下查询后得到0个结果:

  Cursor cur = databaseHelper.getDebtsByConcept(0);

执行查询的地方如下:

public Cursor getDebtsByConcept(int conceptID) {
     String query = "select con.name, per1.name, per2.name, deb.origamt, deb.paidsofar, deb.daterepaid" +
            " from " +
            TABLE_PERSON + " per1, " + TABLE_PERSON + " per2, " +  TABLE_CONCEPT + " con, " + TABLE_DEBT + " deb" +
            " where deb.concept = " + conceptID +
            " and con.id = deb.concept " +
            " and per1.id = deb.creditorID" +
            " and per2.id = deb.debtorID;";

     Cursor res = db.rawQuery(query, null);
     return res;
}

【问题讨论】:

    标签: android sql database sqlite


    【解决方案1】:

    数据库中不会有任何conecptID 值为0 的条目,同样的原因是您已将concept table 中的conceptID 声明为Integer Auto Increment,因此当您插入第一条记录时,值将是conceptId 值将是1

    您可以尝试在查询中传递 1,然后只是检查一下,尝试使用 select * from .... 从数据库中获取所有记录

    【讨论】:

    • 使用 conceptID == 1 搜索也返回 0 个条目。执行“从债务中选择 *”返回一个条目(唯一的一个): (col 0)->1, (col 1)->0, (col 2)->0, (col 3)->0 , (col 4)->10000, (col 5)->100, (col 6)->xxx,
    • 数据好像有些不匹配,能不能做select * from ..其他表,看看你得到了什么结果?
    • 我认为发生了什么很明显。债务表中的外键无处指向:>>DEBTS>PERSONS>CONCEPTS
    • 我终于解决了。谢谢你。这个问题是最明显的。
    猜你喜欢
    • 2013-08-06
    • 1970-01-01
    • 2016-07-18
    • 2015-05-08
    • 2019-12-23
    • 2014-06-15
    • 1970-01-01
    • 2021-08-22
    • 2015-08-26
    相关资源
    最近更新 更多