01-25 23:51:14.507: WARN/System.err(616): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
我有一个使用SQLite
数据库的android应用程序。但当它到达select语句时,它会强制关闭。我有一个用于执行此操作的类文件:
package com.thesis.menubook;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DBConnect {
int id = 0;
public static final String KEY_ROWID = "_id";
public static final String KEY_IP = "saved_ip_address";
private static final String TAG = "DBConnect";
private static final String DATABASE_NAME = "MenuBook";
private static final String DATABASE_TABLE_1 = "ipaddress";
//private static final String DATABASE_TABLE_2 = "menudb";
//private static final String DATABASE_TABLE_3 = "recipelist";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE_TABLE_1 =
"CREATE TABLE ipaddress (_id integer primary key autoincrement, " +
"saved_ip_address text not null " +
"); ";
private static final String DATABASE_CREATE_TABLE_2 =
"CREATE TABLE menudb (menu_ID varchar primary key not null, " +
"menu_name longtext, " +
"menu_price double null default, " +
"menu_description longtext, " +
"menu_category text, " +
"menu_status text " +
"); ";
private static final String DATABASE_CREATE_TABLE_3 =
"CREATE TABLE recipelist (recipe_ID integer primary key not null autoincrement, " +
"menu_ID varchar null default, " +
"stock_ID varchar null default, " +
"recipe_quantity double null defualt " +
");" ;
private final Context context;
private static DatabaseHelper DBHelper;
private static SQLiteDatabase db;
public DBConnect(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE_TABLE_1);
db.execSQL(DATABASE_CREATE_TABLE_2);
db.execSQL(DATABASE_CREATE_TABLE_3);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS ipaddress");
db.execSQL("DROP TABLE IF EXISTS menudb");
db.execSQL("DROP TABLE IF EXISTS recipelist");
onCreate(db);
}
}
//---opens the database---
public DBConnect open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertIPAddress(String ipaddress)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_IP, ipaddress);
return db.insert(DATABASE_TABLE_1, null, initialValues);
}
public String getIP()
{
String retrievedIP;
String query = "SELECT saved_ip_address FROM ipaddress WHERE _id = (SELECT MAX(_id) FROM ipaddress)";
Cursor cursor = db.rawQuery(query, null);
retrievedIP= cursor.getString(cursor.getColumnIndex("saved_ip_address"));
cursor.close();
return retrievedIP;
}
public List<ColumnValue[]> select(final String query)
{
List<ColumnValue[]> result = null;
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst())
{
result = new ArrayList<ColumnValue[]>();
do
{
int columns = cursor.getColumnCount();
ColumnValue[] cvarray = new ColumnValue[columns];
for (int i=0; i<columns; i++)
{
String key = cursor.getColumnName(i);
String value = cursor.getString(i);
ColumnValue cv = new ColumnValue(key, value);
cvarray[i] = cv;
}
result.add(cvarray);
}
while (cursor.moveToNext());
}
cursor.close();
return result;
}
public class ColumnValue
{
public String column, value;
public ColumnValue(String c, String v)
{
column = c; value = v;
}
}
}
我认为这是getID()
条款,但我看不出它有什么问题,也没有看到我的SELECT语句。我在这里要做的是获取位于saved_ip_address
表格底部的ipaddress
的值。
这是我的LogCat,错误的开始以粗体显示。
谢谢
01-25 23:51:14.507:WARN / System.err(616):android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为2
01-25 23:51:14.568: WARN/System.err(616): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:421)
01-25 23:51:14.568: WARN/System.err(616): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:117)
01-25 23:51:14.578: WARN/System.err(616): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:31)
01-25 23:51:14.590: WARN/System.err(616): at com.thesis.menubook.DBConnect.getIP(DBConnect.java:113)
01-25 23:51:14.590: WARN/System.err(616): at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:103)
01-25 23:51:14.622: WARN/System.err(616): at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:51:14.622: WARN/System.err(616): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:51:14.622: WARN/System.err(616): at android.os.Looper.loop(Looper.java:132)
01-25 23:51:14.648: WARN/System.err(616): at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:51:14.688: WARN/System.err(616): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:51:14.688: WARN/System.err(616): at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:51:14.688: WARN/System.err(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:51:14.688: WARN/System.err(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:51:14.688: WARN/System.err(616): at dalvik.system.NativeStart.main(Native Method)
01-25 23:51:14.998: DEBUG/AndroidRuntime(616): Shutting down VM
01-25 23:51:15.008: WARN/dalvikvm(616): threadid=1: thread exiting with uncaught exception (group=0x40014760)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): FATAL EXCEPTION: main
01-25 23:51:15.138: ERROR/AndroidRuntime(616): java.lang.IllegalArgumentException: Host name may not be null
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at com.thesis.menubook.JSONParser.makeHttpRequest(JSONParser.java:62)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at com.thesis.menubook.ChooseTable$GetTableDetails$1.run(ChooseTable.java:119)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at android.os.Handler.handleCallback(Handler.java:587)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at android.os.Looper.loop(Looper.java:132)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at android.app.ActivityThread.main(ActivityThread.java:4025)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at java.lang.reflect.Method.invoke(Method.java:491)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-25 23:51:15.138: ERROR/AndroidRuntime(616): at dalvik.system.NativeStart.main(Native Method)
01-25 23:51:15.328: WARN/ActivityManager(83): Force finishing activity com.thesis.menubook/.ChooseTable
01-25 23:51:15.958: WARN/ActivityManager(83): Activity pause timeout for ActivityRecord{408b4228 com.thesis.menubook/.ChooseTable}
01-25 23:51:20.048: INFO/Process(616): Sending signal. PID: 616 SIG: 9
01-25 23:51:21.068: INFO/ActivityManager(83): Process com.thesis.menubook (pid 616) has died.
01-25 23:51:21.089: ERROR/InputDispatcher(83): channel '408408d0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Consumer closed input channel or an error occurred. events=0x8
01-25 23:51:21.098: ERROR/InputDispatcher(83): channel '408408d0 com.thesis.menubook/com.thesis.menubook.IPAddress (server)' ~ Channel is unrecoverably broken and will be disposed!
01-25 23:51:21.768: INFO/WindowManager(83): WIN DEATH: Window{408408d0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:51:22.108: INFO/WindowManager(83): WIN DEATH: Window{407b66d8 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:51:22.246: INFO/WindowManager(83): WIN DEATH: Window{4097d668 com.thesis.menubook/com.thesis.menubook.ChooseTable paused=false}
01-25 23:51:22.358: INFO/WindowManager(83): WINDOW DIED Window{408408d0 com.thesis.menubook/com.thesis.menubook.IPAddress paused=false}
01-25 23:51:22.449: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel. fd=188, events=0x8
01-25 23:51:22.449: ERROR/InputDispatcher(83): Received spurious receive callback for unknown input channel. fd=190, events=0x8
01-25 23:51:23.468: DEBUG/dalvikvm(147): GC_EXPLICIT freed <1K, 4% free 12942K/13383K, paused 285ms+19ms
01-25 23:51:23.858: WARN/InputManagerService(83): Got RemoteException sending setActive(false) notification to pid 616 uid 10004
01-25 23:52:15.278: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol
01-25 23:57:15.589: DEBUG/SntpClient(83): request time failed: java.net.SocketException: Address family not supported by protocol
答案 0 :(得分:1)
Cursor
对象后,您需要前进到第一行...
cursor.moveToFirst();
从光标中取出之前。你还需要检查光标中是否确实存在任何行。