【问题标题】:no such column error - sqlite没有这样的列错误 - sqlite
【发布时间】:2015-09-07 06:56:56
【问题描述】:

我收到一个奇怪的错误:

E/SQLiteLog﹕ (1) no such column: Test
06-22 01:51:20.006  15089-15089/com.almas.mehr.sms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: no such column: Test (code 1): , while compiling: SELECT * FROM groups WHERE title = Test
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
            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:44)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1359)
            at com.almas.mehr.databases.MainDB.isSameGroup(MainDB.java:93)
            at com.almas.mehr.submain.Groups.onClick(Groups.java:99)
            at android.view.View.performClick(View.java:4354)

数据库类:

//database name
private final static String DATABASE_NAME   = "almasmehrsms" ;

//tables name
private final String GROUPS         = "groups";
private final String NOTIFICATIONS  = "notifications";

//columns of groups table
private final String  ID_GROUPS     =   "_id";
private final String  TITLE_GROUPS  =   "title";
private final String  DATE_GROUPS   =   "gdate";

//create groups table
private final String CREATE_GROUPS_TABLE = "CREATE TABLE " + GROUPS + "("
        + ID_GROUPS + " INTEGER PRIMARY KEY AUTOINCREMENT," + TITLE_GROUPS + " TEXT," +
        DATE_GROUPS + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP" + ")";



public MainDB(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_GROUPS_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
    db.execSQL("DROP TABLE IF EXISTS " + GROUPS);
    db.execSQL("DROP TABLE IF EXISTS " + NOTIFICATIONS);
    onCreate(db);
}


public Boolean isSameGroup(String title){
    Boolean issamegroup = true ;
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS +" = " + title;
    Cursor cursor = db.rawQuery(query, null);
    cursor.moveToFirst();
    if(cursor.getCount() > 0) {
        issamegroup = true ;
    }else{

        issamegroup = false ;
    }

    return issamegroup ;
}

我写了这个函数来检查一个值是否重复:

public Boolean isSameGroup(String title){

...

}

例如我来自EditText 的输入是Test,我收到此消息错误:

 E/SQLiteLog﹕ (1) no such column: Test

【问题讨论】:

  • 标题值(test)需要加引号

标签: android sql sqlite select android-sqlite


【解决方案1】:

将您的 WHERE 子句更改为...

... 
title = 'test' 

它的编写方式是查找名为 Test 的列。

【讨论】:

    【解决方案2】:

    SQL 中的字符串文字用单引号 (') 表示。没有它们,字符串将被视为对象名称。在这里,您生成一个 where 子句 title = Test。两者都被解释为列名,查询失败,因为没有列Test

    要解决这个问题,您可以用引号将Test 括起来:

    String query = "SELECT * FROM "+ GROUPS +" WHERE "+ TITLE_GROUPS + " = '" + title + "'";
    

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      相关资源
      最近更新 更多