Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t查询androidsqlite数据库_Android_Sqlite - Fatal编程技术网

Can';t查询androidsqlite数据库

Can';t查询androidsqlite数据库,android,sqlite,Android,Sqlite,我在LogCat中不断得到以下异常: >05-07 11:26:19.276: E/AndroidRuntime(1608): FATAL EXCEPTION: main 05-07 11:26:19.276: E/AndroidRuntime(1608): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db_003/com.db_003.MainActivityDb3}: java.lang.

我在LogCat中不断得到以下异常:

>05-07 11:26:19.276: E/AndroidRuntime(1608): FATAL EXCEPTION: main
05-07 11:26:19.276: E/AndroidRuntime(1608): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db_003/com.db_003.MainActivityDb3}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-07 11:26:19.276: E/AndroidRuntime(1608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-07 11:26:19.276: E/AndroidRuntime(1608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.os.Looper.loop(Looper.java:137)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at dalvik.system.NativeStart.main(Native Method)
05-07 11:26:19.276: E/AndroidRuntime(1608): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.database.CursorWindow.nativeGetLong(Native Method)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.database.CursorWindow.getLong(CursorWindow.java:507)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.database.CursorWindow.getInt(CursorWindow.java:574)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at com.db_003.DB_Helper.cursorToList(DB_Helper.java:88)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at com.db_003.DB_Helper.queryCustom(DB_Helper.java:105)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at com.db_003.MainActivityDb3.onCreate(MainActivityDb3.java:29)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.Activity.performCreate(Activity.java:5008)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-07 11:26:19.276: E/AndroidRuntime(1608):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-07 11:26:19.276: E/AndroidRuntime(1608):     ... 11 more>
这是我的查询代码:

    Log.d("Select: ", "All Kerry towns");
    String table = DB_Helper.TABLE_TOWNS;
    String where = " WHERE " + DB_Helper.COUNTY + " = 'Kerry'";
    String strSQL = "SELECT " + DB_Helper.COUNTY + " FROM " + table + where + ";";
    Log.d("Select SQL Statement: ", strSQL);
    List<Town> towns = db.queryCustom( strSQL );
    for ( Town t : towns ) {
        Log.d("Kerry: ", t.toString() );
    }
以下是DB_Helper.java中的查询方法:

public List<Town> queryCustom(String strSQL) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor =  db.rawQuery(strSQL, null);
    List<Town> townList = cursorToList( cursor );
    db.close();
    return townList;        
}
public List queryCustom(字符串strSQL){
SQLiteDatabase db=getReadableDatabase();
Cursor=db.rawQuery(strSQL,null);
List townList=游标TOLIST(游标);
db.close();
返回城市名单;
}
和cursorToList方法:

private List<Town> cursorToList(Cursor cursor) {
    List<Town> townList = new ArrayList<Town>();
    if ( cursor.moveToFirst() ) {
        do {
            // get the field details from the cursor                
            int id = cursor.getInt( cursor.getColumnIndex(_ID) );
            String town = cursor.getString( cursor.getColumnIndex( TOWN) );
            String county = cursor.getString( cursor.getColumnIndex( COUNTY) );
            String province = cursor.getString( cursor.getColumnIndex( PROVINCE) );
            String country = cursor.getString( cursor.getColumnIndex( COUNTRY) );
            // create a new empty instance of
            // Country and add it to the list
            townList.add( new Town(id, town, county, province, country) );
        } while ( cursor.moveToNext() );
    }
    return townList;
}
私有列表游标列表(游标){
List townList=new ArrayList();
if(cursor.moveToFirst()){
做{
//从光标获取字段详细信息
int id=cursor.getInt(cursor.getColumnIndex(_id));
String town=cursor.getString(cursor.getColumnIndex(town));
String county=cursor.getString(cursor.getColumnIndex(county));
stringprovince=cursor.getString(cursor.getColumnIndex(province));
字符串country=cursor.getString(cursor.getColumnIndex(country));
//创建的新空实例
//并将其添加到列表中
城镇列表。添加(新城镇(id、城镇、县、省、国家));
}while(cursor.moveToNext());
}
返回城市名单;
}
这在选择所有列时效果很好,但我无法选择特定列。非常感谢您的帮助。

您的
游标列表()
假定所有5列都在
游标中。当您只查询一列时,将得到-1作为缺少的列的列索引,并将该索引与
get…()
一起使用,则会出现异常


在尝试使用索引之前,请始终查询所有列,或者检查索引是否不是-1。

很明显,此方法调用返回了-1

cursor.getColumnIndex( COUNTY ) 
表中有数据吗?也许某处有拼写错误,
“郡”和“国”很容易混淆

这样做吧,希望对你有用

public List<Town> cursorToList(Cursor cursor) {
        List<Town> townList = new ArrayList<Town>();
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> col = new HashMap<String, String>();
                int size = cursor.getColumnCount();
                for (int i = 0; i < size; i++) {
                    col.put(cursor.getColumnName(i), cursor.getString(i));
                    townList.add(new Town(col.get(_ID), col.get(TOWN), col.get(COUNTY), col.get(PROVINCE), col.get(COUNTRY)));
                }
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return townList;
    }
公共列表游标列表(游标){
List townList=new ArrayList();
if(cursor.moveToFirst()){
做{
HashMap col=新的HashMap();
int size=cursor.getColumnCount();
对于(int i=0;i
cursor.getColumnIndex(\u ID)
返回-1。。。猜猜原因(或阅读文档).
很明显-1正通过此方法调用游标返回。getColumnIndex(COUNTY)
否,它不是。。。“选择查询”中有一个county列。。。没有id_uu和其他人…谢谢,这似乎起到了作用。更改了cursorToList方法以仅获取TOWN列。
public List<Town> cursorToList(Cursor cursor) {
        List<Town> townList = new ArrayList<Town>();
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> col = new HashMap<String, String>();
                int size = cursor.getColumnCount();
                for (int i = 0; i < size; i++) {
                    col.put(cursor.getColumnName(i), cursor.getString(i));
                    townList.add(new Town(col.get(_ID), col.get(TOWN), col.get(COUNTY), col.get(PROVINCE), col.get(COUNTRY)));
                }
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return townList;
    }