我尝试从android中的SQLite数据库中检索一些数据,但是我收到了一些错误。
首先,这是我的数据库代码片段:
private static final String TABLE_NAME_UrlCache = "UrlCache";
private static final String COLUMN_NAME_UrlCache_ID = "id";
private static final String COLUMN_NAME_UrlCache_TIME = "time";
private static final String COLUMN_NAME_UrlCache_DATE = "date";
private static final String COLUMN_NAME_UrlCache_CLIENTIP = "clientip";
private static final String COLUMN_NAME_UrlCache_CLIENTMAC = "clientmac";
private static final String COLUMN_NAME_UrlCache_REQUESTEDURL = "requestedurl";
private static final String COLUMN_NAME_UrlCache_FILENAME = "filename";
private static final String CREATE_TABLE_UrlCache = "CREATE TABLE " +TABLE_NAME_UrlCache+ " (" + COLUMN_NAME_UrlCache_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME_UrlCache_TIME +" text, " + COLUMN_NAME_UrlCache_DATE + " text, "+ COLUMN_NAME_UrlCache_CLIENTIP + " text, "+ COLUMN_NAME_UrlCache_CLIENTMAC + " text, "+ COLUMN_NAME_UrlCache_REQUESTEDURL +" text, "+COLUMN_NAME_UrlCache_FILENAME+" text "+");";
public boolean addRequestedURL(String time,String date,String client_ip,String client_mac,String url,String file_name)
{
Log.d("Client mac db", client_mac);
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(this.COLUMN_NAME_UrlCache_TIME, time);
contentValues.put(this.COLUMN_NAME_UrlCache_DATE, date);
contentValues.put(this.COLUMN_NAME_UrlCache_CLIENTIP, client_ip);
contentValues.put(this.COLUMN_NAME_UrlCache_CLIENTMAC, client_mac);
contentValues.put(this.COLUMN_NAME_UrlCache_REQUESTEDURL, url);
contentValues.put(this.COLUMN_NAME_UrlCache_FILENAME, file_name);
db.insert(TABLE_NAME_UrlCache, null, contentValues);
return true;
}
public boolean FileFromUrlAlreadyRequested(String url)
{
SQLiteDatabase db = this.getReadableDatabase();
try
{
Cursor cursor = db.rawQuery("select requestedurl from "+ TABLE_NAME_UrlCache, null);
try {
if (cursor.moveToFirst())
{
do
{
String retrieved_url = cursor.getString(cursor.getColumnIndex(DBManager.COLUMN_NAME_UrlCache_REQUESTEDURL));
String retrieved_id = cursor.getString(cursor.getColumnIndex(DBManager.COLUMN_NAME_UrlCache_CLIENTMAC));
if (retrieved_url.equals(url))
{
this.setID(retrieved_id);
return true;
}
} while (cursor.moveToNext());
}
}
finally
{
try
{
cursor.close();
}
catch (Exception ignore) {}
}
}
finally
{
try
{
db.close();
} catch (Exception ignore) {}
}
return false;
}
这是错误:
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.android.python27.DBManager.FileFromUrlAlreadyRequested(DBManager.java:427)
at com.android.python27.Download$GetRequestedURLAsyncTask.onPostExecute(Download.java:267)
at com.android.python27.Download$GetRequestedURLAsyncTask.onPostExecute(Download.java:193)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
当我尝试在这里检索mac地址时它崩溃了:
String retrieved_id = cursor.getString(cursor.getColumnIndex(DBManager.COLUMN_NAME_UrlCache_CLIENTMAC));
我检查了Lod.d以查看我是否成功传递了变量,因为我可以看到新记录是成功创建的。
任何建议都会有所帮助并深表感激。
答案 0 :(得分:2)
您尝试阅读DBManager.COLUMN_NAME_UrlCache_CLIENTMAC
列,但不在投影列表中。
更改您的SQL查询以同时选择DBManager.COLUMN_NAME_UrlCache_CLIENTMAC
和DBManager.COLUMN_NAME_UrlCache_REQUESTEDURL
。
答案 1 :(得分:0)
您的变量被声明为“私有”,这意味着它只能在课程中访问,请尝试将其更改为“公共”。