选择SQLite和android的位置

时间:2016-05-12 13:15:09

标签: java android sqlite

嘿,我目前有两个函数,一个用字符串打印整个数据库,另一个用于在数据库的“TODO”列中满足条件时只获取数据,如下所示。

     public String databaseToString() {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_LIST + " WHERE 1";

    //Cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    //Move to the first row in your results
    c.moveToFirst();

    //Position after the last row means the end of the results
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("ToDo")) != null) {
            dbString += c.getString(c.getColumnIndex("ToDo"));
            dbString += " ";
            dbString += c.getString(c.getColumnIndex("accFor"));
            dbString += "\n";
        }
        c.moveToNext();
    }
    db.close();
    return dbString;
}

这是工作函数的代码,它将整个数据库转换为要打印的字符串。下面是无法工作的函数,它应该从数据库中获取所有值,其中TODO等于我在mydatabasemanager app中进一步调用的字符串大学,其中包含这两个函数

 public String UniversityToString() {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = ("SELECT " + COLUMN_TODO + COLUMN_FOR + " FROM " + TABLE_LIST + " WHERE "+ COLUMN_FOR + "=\"" + University + "\";");

    //Cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    //Move to the first row in your results
    c.moveToFirst();

    //Position after the last row means the end of the results
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("ToDo")) != null) {
            dbString += c.getString(c.getColumnIndex("ToDo"));
            dbString += " ";
            dbString += c.getString(c.getColumnIndex("accFor"));
            dbString += "\n";
        }
        c.moveToNext();
    }

    db.close();
    return dbString;
}

这里的代码导致我的应用程序在打开调用它的活动时崩溃我只是想知道如何解决这个问题以正确选择我需要的数据。

1 个答案:

答案 0 :(得分:0)

查询中的两列之间至少应该有一些内容。

COLUMN_TODO + ", " COLUMN_FOR

您也不需要添加;在查询字符串的末尾