Android sqlite查询给出了一个错误,该列肯定不存在

时间:2015-04-05 23:38:06

标签: android mysql sqlite

我在下面的SQL语句中收到错误。导致错误的方法是查找" Anniversary"在列"名称"从下表所示的表中。如果找不到这样的行,它将添加一行。该错误似乎表明该表没有列" Anniversary"但我没有尝试查询列周年纪念日,但我正在尝试查询列"名称。"完全糊涂了。任何帮助表示赞赏。对不起所有的代码,但这一切看起来都很相关,所以我试着把它分解成部分。

我在运行时遇到以下错误:

  Caused by: android.database.sqlite.SQLiteException: no such column: Anniversary (code 1): , while compiling: SELECT * FROM special_days WHERE name = Anniversary
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:919)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:530)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1379)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1226)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1097)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1265)
        at com.mycompany.dudesmyreminders.dbDataSource.queryToList(dbDataSource.java:147)
        at com.mycompany.dudesmyreminders.Significant_OtherActivity.populateAnniv(Significant_OtherActivity.java:140)
   ...

导致错误的方法:

public void populateAnniv() {
    List<dbData> tempDBDatas;
    tempDBDatas = datasource.queryToList(dbSDTable,null,
            dbSDColumnName + " = " + dbSDFieldAnniv,
            null,null,null,null);
    if (tempDBDatas.size() > 0) {
        dbDataAnniv = tempDBDatas.get(0);
    } else {
        dbDataAnniv = datasource.add_Row(dbSDFieldAnniv, dbSDTable, dbSDColumnName);
        datasource.set_ColumnVal(dbDataAnniv, dbSDColumnDate, "10 29 1929");
        datasource.set_ColumnVal(dbDataAnniv, dbSDColumnWarn, 10);
    }
    TextView textViewAnniv = ( TextView ) findViewById( R.id.SignificantOther_selectAnni);
    textViewAnniv.setText(datasource.get_Column_StrVal(dbDataAnniv, dbSDColumnDate));

}

public List<dbData> queryToList(String myTable, String[] myColumns, String whereClause,
                                     String[] whereArgs, String groupBy, String having, String orberBy) {
    System.out.println("start queryToList");
    List<dbData> dbDatas = new ArrayList<>();

    Cursor cursor = database.query(myTable, myColumns,
            whereClause, whereArgs, groupBy, having, orberBy);
    System.out.println("cursor count = " + cursor.getCount());
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        dbData dbData = cursorToDB_Row(cursor, myTable);
        dbData.setIntTemp1(0);
        dbDatas.add(dbData);
        cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return dbDatas;

}

方法使用的变量:

private String dbSOTable = MySQLiteHelper.dbFields.TABLE_NAME_SIGNIFICANT_OTHER;
private String dbSOColumnName = MySQLiteHelper.dbFields.COLUMN_SIGNIFICANT_OTHER_NAME;
private String dbSDTable = MySQLiteHelper.dbFields.TABLE_NAME_SPECIAL_DAYS;
private String dbSDColumnName = MySQLiteHelper.dbFields.COLUMN_SPECIAL_DAYS_NAME;
private String dbSDFieldAnniv = MySQLiteHelper.dbFields.FIELD_SPECIAL_DAY_RESERVED_ANNIVERSARY;
private String dbSDFieldBirth = MySQLiteHelper.dbFields.FIELD_SPECIAL_DAY_RESERVED_BIRTHDAY;
private String dbSDColumnDate = MySQLiteHelper.dbFields.COLUMN_SPECIAL_DAYS_DATE;
private String dbSDColumnWarn = MySQLiteHelper.dbFields.COLUMN_SPECIAL_DAYS_WARNING;

来自SQLiteHelper:

/* Inner class that defines the table contents */
public static abstract class dbFields implements BaseColumns {
    //Special_Days Table Fields
    public static final String TABLE_NAME_SPECIAL_DAYS = "special_days";
    public static final String COLUMN_SPECIAL_DAYS_ID = _ID;
    public static final String COLUMN_SPECIAL_DAYS_DATE = "date";  //dbDataRow strField 1
    public static final String COLUMN_SPECIAL_DAYS_NAME = "name"; //dbDataRow dbData
    public static final String COLUMN_SPECIAL_DAYS_ALTITUDE = "altitude"; //dbDataRow intField 1
    public static final String COLUMN_SPECIAL_DAYS_USED = "is_used"; //dbDataRow Field 2
    public static final String COLUMN_SPECIAL_DAYS_WARNING = "warning"; //dbDataRow intField 3
    public static final String COLUMN_SPECIAL_DAYS_ACTION = "action"; //dbDataRow intField 4

}

//Database creation sql statement
private static final String SQL_CREATE_SPECIAL_DAYS =
        "CREATE TABLE " + dbFields.TABLE_NAME_SPECIAL_DAYS + " (" +
                dbFields.COLUMN_SPECIAL_DAYS_ID + INTEGER_PRIMARY_KEY + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_NAME + TEXT_TYPE + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_DATE + TEXT_TYPE + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_ALTITUDE + INTEGER_TYPE + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_USED + INTEGER_TYPE + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_WARNING + INTEGER_TYPE + COMMA_SEP +
                dbFields.COLUMN_SPECIAL_DAYS_ACTION + INTEGER_TYPE +
                // Any other options for the CREATE command
                " )";

1 个答案:

答案 0 :(得分:1)

如果将dbSDFieldAnniv包裹在'...'中会怎样?

tempDBDatas = datasource.queryToList(dbSDTable,null,
        dbSDColumnName + " = '" + dbSDFieldAnniv + "'",
        null,null,null,null);