SQL确实存在时没有这样的列错误

时间:2012-08-01 19:53:57

标签: android android-sqlite

当我查询我的数据库时,我一直收到错误SQLException: no such column,我不明白为什么我的查询看起来很好

Cursor nameCursor = context.getContentResolver().query(StatusList.STATUS_URI,new String[] {StatusList.STATUS_NAME,StatusList.STATUS_PERMISSIONS,StatusList.STATUS_ECM2ID}
                ,StatusList.STATUS_TYPE+"=1"+" AND "+StatusList.STATUS_NAME+"="+incidentCursor.getString(0),null,null);

这是完整的错误

08-01 15:39:59.104: W/System.err(8623): android.database.sqlite.SQLiteException: no such column: SIS (code 1): , while compiling: SELECT name, permissions, ecm2id FROM Status WHERE (type=1 AND name=SIS)
08-01 15:39:59.112: W/System.err(8623):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
08-01 15:39:59.112: W/System.err(8623):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
08-01 15:39:59.120: W/System.err(8623):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
08-01 15:39:59.128: W/System.err(8623):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:400)
08-01 15:39:59.128: W/System.err(8623):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:294)
08-01 15:39:59.128: W/System.err(8623):     at ecm2.android.Providers.StatusList.query(StatusList.java:132)
08-01 15:39:59.128: W/System.err(8623):     at android.content.ContentProvider.query(ContentProvider.java:652)
08-01 15:39:59.128: W/System.err(8623):     at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
08-01 15:39:59.128: W/System.err(8623):     at android.content.ContentResolver.query(ContentResolver.java:370)
08-01 15:39:59.135: W/System.err(8623):     at android.content.ContentResolver.query(ContentResolver.java:313)
08-01 15:39:59.135: W/System.err(8623):     at ecm2.android.MainActivity.createNewDialog(MainActivity.java:1167)
08-01 15:39:59.135: W/System.err(8623):     at ecm2.android.MainActivity$ListViews.onListItemClick(MainActivity.java:641)
08-01 15:39:59.135: W/System.err(8623):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
08-01 15:39:59.135: W/System.err(8623):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-01 15:39:59.135: W/System.err(8623):     at  android.widget.AbsListView.performItemClick(AbsListView.java:1086)
08-01 15:39:59.143: W/System.err(8623):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
08-01 15:39:59.143: W/System.err(8623):     at android.widget.AbsListView$1.run(AbsListView.java:3529)
08-01 15:39:59.143: W/System.err(8623):     at android.os.Handler.handleCallback(Handler.java:615)
08-01 15:39:59.143: W/System.err(8623):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 15:39:59.143: W/System.err(8623):     at android.os.Looper.loop(Looper.java:137)
08-01 15:39:59.143: W/System.err(8623):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-01 15:39:59.143: W/System.err(8623):     at java.lang.reflect.Method.invokeNative(Native Method)
08-01 15:39:59.151: W/System.err(8623):     at java.lang.reflect.Method.invoke(Method.java:511)
08-01 15:39:59.151: W/System.err(8623):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 15:39:59.151: W/System.err(8623):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 15:39:59.151: W/System.err(8623):     at dalvik.system.NativeStart.main(Native Method)

基本上我要做的是查询我的数据库,查找type 1name

的任何内容

我知道这样的记录存在,所以我不知道为什么我会收到此错误

编辑: 数据库创建代码

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        createTables(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                          int newVersion) 
    {
        Log.w("Messages", "Upgrading database from version " + oldVersion 
              + " to "
              + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS Incidents");
        db.execSQL("DROP TABLE IF EXISTS Distributions");
        onCreate(db);
    }

    private void createTables(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + INCIDENTS_TABLE + "(" + INCIDENT_ID + " integer primary key autoincrement, " +
                INCIDENT_ECM2ID + " INTEGER, " + INCIDENT_STATON_NAME + " TEXT, " + INCIDENT_READ + " INTEGER, " + INCIDENT_TOC + " TEXT, " +
                INCIDENT_STATION_ID + " INTEGER, " + INCIDENT_MESSAGE_ID + " INTEGER, " + INCIDENT_MESSAGE + " TEXT);");

        db.execSQL("CREATE TABLE " + DISTRIBUTION_MESSAGES_TABLE + "(" + DIST_ID + " integer primary key autoincrement, " +
                DIST_MESSAGE_ID + " INTEGER, " + DIST_ECM2ID + " INTEGER, " + DIST_SENDER_EMAIL + " TEXT, " + 
                DIST_SENDER_NAME + " TEXT, " + DIST_STATION_ID + " INTEGER, " + DISC_READ + " INTEGER, " + DIST_TOC + " TEXT, "
                + DIST_SUBJECT +" TEXT, " + DIST_MESSAGE + " TEXT);");
    }
}

2 个答案:

答案 0 :(得分:1)

SIS不是列,它应该是一个字符串。使用?替换字符或将字符串包装在引号中。

使用SQL注入安全?

Cursor nameCursor = context.getContentResolver().query(StatusList.STATUS_URI,
        new String[] {StatusList.STATUS_NAME, StatusList.STATUS_PERMISSIONS, StatusList.STATUS_ECM2ID},
        StatusList.STATUS_TYPE + "=1 AND " + StatusList.STATUS_NAME + "=?",
        new String[] {incidentCursor.getString(0)},
        null);

使用引号:

Cursor nameCursor = context.getContentResolver().query(StatusList.STATUS_URI,
        new String[] {StatusList.STATUS_NAME, StatusList.STATUS_PERMISSIONS, StatusList.STATUS_ECM2ID},
        StatusList.STATUS_TYPE + "=1 AND " + StatusList.STATUS_NAME + "='" + incidentCursor.getString(0) + "'",
        null,
        null);

答案 1 :(得分:1)

以下是构建查询的正确方法:

Cursor nameCursor = context.getContentResolver().query(
        StatusList.STATUS_URI,
        new String[] { StatusList.STATUS_NAME, StatusList.STATUS_PERMISSIONS, StatusList.STATUS_ECM2ID },
        StatusList.STATUS_TYPE + "=?" + " AND " + StatusList.STATUS_NAME + "=?",
        new String[] { "1", incidentCursor.getString(0) },
        null);