我正在从网站下载数据库。下载的数据库被调用db.php
,它将存储在 name 下的 data/data/my.package.name/files 中FishingMatey.db
。该数据库位于 DDMS 的文件系统中,我可以在 PC 上的 SQLite Studio 中打开它。在那里,我的数据库充满了正确的表格和正确的数据。这是我下载 SQLite 数据库的代码:
public boolean downloadDatabase() {
try {
// Log.d(TAG, "downloading database");
URL url = new URL("http://myurl.com/db.php");
// Open a connection to that URL */
URLConnection ucon = url.openConnection();
// Define InputStreams to read from the URLConnection
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
// Read bytes to the Buffer until there is nothing more to read(-1)
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
// Convert the Bytes read to a String
FileOutputStream fos = null;
// Select storage location
fos = this.context.openFileOutput(DATABASE_NAME, Context.MODE_PRIVATE);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.e("downloadDatabase", "downloadDatabase Error: ", e);
return false;
} catch (NullPointerException e) {
Log.e("downloadDatabase", "downloadDatabase Error: ", e);
return false;
} catch (Exception e) {
Log.e("downloadDatabase", "downloadDatabase Error: ", e);
return false;
}
return true;
}
我的问题:我可以用 打开数据库
SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.menuswitcher/files/" + DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
,但是如果我输入db.query(DATABASE_TABLE_Bewirtschafter, null, null, null, null, null, null);
我会收到以下错误:
01-08 19:52:22.366: E/AndroidRuntime(6157): FATAL EXCEPTION: main
01-08 19:52:22.366: E/AndroidRuntime(6157): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
这是我在 Activity 中的调用方式: this.b3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DBAccess dbAccess = new DBAccess(HauptmenueActivity.this, 1, "FishingMatey.db");
if (dbAccess.downloadDatabase()) {
dbAccess.initDatabase();
Cursor cur = dbAccess.createBewirtschafterAllCursor();
Log.v("b3", cur.getString(cur.getColumnIndex("name")));
} else {
Log.e("b3", "Error!");
}
}
});
有谁知道为什么这不起作用?