我已經獲得了我的sqlite數據庫文件nhl.db,並將其放入我的項目的資產目錄中。然後我從eclipse運行模擬器並加載程序,但是當我調用db時,我得到'沒有這樣的表'的錯誤。我檢查data/data/myapp/databases /目錄,並且有一個名爲我的數據庫的文件,但是打開檢查它,它是一個空的數據庫文件,顯然是由SqLiteOpenHelper創建的,因爲它無法找到我的數據庫。Android數據庫文件訪問
我的問題是我在哪裏把我的sqlite數據庫文件,以便它被打開,而不是創建?
public class DatabaseHandlerSimple extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "nhl";
// Contacts table name
private static final String TABLE_PLAYERS = "players";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TEAM = "team";
private static final String KEY_NUMBER = "number";
private static final String KEY_FNAME = "firstname";
private static final String KEY_LNAME = "lastname";
private static final String KEY_POSITION = "position";
public DatabaseHandlerSimple(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Getting All Contacts
public List<Player> getAllPlayers() {
List<Player> playerList = new ArrayList<Player>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_PLAYERS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Player player = new Player();
player.setID(Integer.parseInt(cursor.getString(0)));
player.setTeam(Integer.parseInt(cursor.getString(1)));
player.setNumber(Integer.parseInt(cursor.getString(2)));
player.setFirstName(cursor.getString(3));
player.setLastName(cursor.getString(4));
player.setPosition(Integer.parseInt(cursor.getString(5)));
// Adding contact to list
playerList.add(player);
} while (cursor.moveToNext());
}
// return contact list
return playerList;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
做一個快速的谷歌搜索讓我有很多點擊:https://www.google.se/search?q=andorid+import+external+sqlite+database&sugexp=chrome,mod=6&sourceid=chrome&ie=UTF-8#hl = EN&TBO = d&拼寫= 1&q =機器人+進口+外部+源碼+數據庫&SA = X&EI = ZeQLUbzdD4em4AS8q4DwBg&VED = 0CCoQvwUoAA&BAV = on.2,or.r_gc.r_pw.r_cp.r_qf。&BVM = bv.41867550,d.bGE&FP = 156ca75c7c80a8d4&BIW = 1855&波黑= 994 –
這又如何呢? http://stackoverflow.com/questions/2211248/using-custom-file-for-android-sqlite-database – ATom
你試過把它,就像我不知道,在數據庫文件夾? – njzk2