The Hawk
The Hawk

Reputation: 1568

Android Database File Access

I've got my sqlite database file, nhl.db, and I put it in the assets directory of my project. Then I run the emulator from eclipse and the program loads, but when I call the db, I get a 'no such table' error. I examine the data/data/myapp/databases/ directory and there is a file with the name of my database, but open examining it, it is an empty database file that was apparently created by the SqLiteOpenHelper because it couldn't find my database.

My question is where do I put my sqlite database file so that it gets opened and not created?

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

}

}

Upvotes: 0

Views: 264

Answers (2)

Raymond P
Raymond P

Reputation: 752

Take a look at this: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

You can drop the .db extension, just name it nhl in the assets folder.

Upvotes: 1

Boe-Dev
Boe-Dev

Reputation: 1595

there is a very good tutorial about SQLite from Vogella, try this to fix it. For me it looks like you did not create a database here.

Upvotes: 1

Related Questions