1

在这里,我将 sqlite 数据库中的文本值与字符串(部分、分支等)进行比较。这是正确的比较方法吗?我在 query() 语句中没有收到这样的列错误。

public Cursor getAllRows(String year, String section, String branch,
                         String subject, String date) {    

  return db.query(
      DATABASE_TABLE,
      null, 
      KEY_YEAR + "=" + year + 
        " AND "+ KEY_SECTION + "=" + section +
        " AND "+ KEY_BRANCH + "=" + branch + 
        " AND "+ KEY_SUBJECT + "=" + subject + 
        " AND "+ KEY_DATE + "=" + date,
      null, 
      null, 
      null, 
      null
  );
} 

日志猫

04-04 10:31:51.568: ERROR/AndroidRuntime(21928):
   Caused by:
     android.database.sqlite.SQLiteException:
       no such column: CSE: ,
       while compiling:
         SELECT * 
         FROM student 
         WHERE year=2 AND branch like CSE 
         AND subject=SE AND date=04-Apr-2012

04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at sharath.android.trail.DBAdapter.getAllRows(DBAdapter.java:100)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at sharath.android.trail.MytableActivity.onCreate(MytableActivity.java:52)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-04 10:31:51.568: ERROR/AndroidRuntime(21928): ... 20 more

实际上 CSE 是属于分支列的一个值,在此先感谢。

4

2 回答 2

2

查询中的字符串常量需要用单引号 (') 字符括起来。您正在插入不带引号的字符串,因此它们被解释为列引用。

通常,直接插入字符串值是个坏主意。您应该创建一个带有绑定参数的语句。

这将自动转义并引用字符串:

public Cursor getAllRows(String year, String section, String branch,
    String subject, String date)
{
    return db.rawQuery("SELECT * FROM " +
         DATABASE_TABLE + " WHERE " +
         KEY_YEAR + "=? AND " +
         KEY_SECTION + "=? AND " +
         KEY_BRANCH + "=? AND " +
         KEY_SUBJECT + "=? AND "+
         KEY_DATE + "=?",
         new String[] { year, section, branch, subject, date }
    );
} 
于 2012-04-04T05:10:43.230 回答
0

你能告诉我 KEY_YEAR,KEY_SECTION,KEY_BRANCH,KEY_SUBJECT,KEY_DATE 是代表列名的字符串还是直接列名,如果这是代表列名的字符串,那么

请试试这个

    return db.query(DATABASE_TABLE,null, 
    KEY_YEAR + "='" + year +"' "
    "AND "+ KEY_SECTION + "='" + section +"' "
    "AND "+ KEY_BRANCH + "='" + branch + "' "
    "AND "+ KEY_SUBJECT + "='" + subject +"' "
    "AND "+ KEY_DATE + "='" + date+"'",
    null, 
    null, 
    null, 
    null);

或者如果这是直接列名,请试试这个

return db.query(DATABASE_TABLE,null, 
         "KEY_YEAR='"+ year +"' AND"
         "KEY_SECTION='"+section +"' AND"
         "KEY_BRANCH='"+branch + "' AND"
         "KEY_SUBJECT='"+subject +"' AND"
         "KEY_DATE='"+date+"'",
         null, 
         null, 
         null, 
         null);
于 2012-04-04T05:41:24.407 回答