0

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

}

}

5
  • Doing a quick google search yielded me many hits: google.se/… Commented Feb 1, 2013 at 15:52
  • And what about this? stackoverflow.com/questions/2211248/… Commented Feb 1, 2013 at 15:53
  • did you try to put it, like, i don't know, in the database folder ? Commented Feb 1, 2013 at 15:57
  • basically, you are telling android "guess where my database is?" and he fails. (surprisingly) Commented Feb 1, 2013 at 15:59
  • I tried putting in the database folder, but it keeps getting overwritten with a new blank database file. Everytime the error is 'no such table'. also the permissions of the new file is -rw-rw---- Commented Feb 1, 2013 at 17:01

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

drop the db extension of the file in the assets folder or in the declaration of the db_name variable?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.