0

我有我的 sqlite 数据库文件 nhl.db,我把它放在我项目的 assets 目录中。然后我从 Eclipse 运行模拟器并加载程序,但是当我调用数据库时,我得到一个“没有这样的表”错误。我检查了 data/data/myapp/databases/ 目录,有一个带有我的数据库名称的文件,但是打开检查它,它是一个空的数据库文件,显然是由 SqLiteOpenHelper 创建的,因为它找不到我的数据库.

我的问题是我应该把我的 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

}

}

4

2 回答 2

1

看看这个:http ://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

您可以删除 .db 扩展名,只需在 assets 文件夹中将其命名为 nhl。

于 2013-02-01T15:54:01.250 回答
1

Vogella有一个关于 SQLite 的非常好的教程,试试这个来修复它。对我来说,您似乎没有在这里创建数据库。

于 2013-02-01T15:52:20.343 回答