0

I have this MainActivity, that calls a class that generates a database.

    public class MainActivity extends Activity {
    PlayerDataSource pds;
    ArrayList<Player>listOfPlayer;
    PlayerAdapter adapter;
    ListView listview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scores);
        listview=(ListView) findViewById(R.id.allPlayers);

        pds=new PlayerDataSource(this);

        pds.open();
}



    }

class PlayerDataSource:

public class PlayerDataSource {

    Context context;
    PlayerOpenHelper helper;
    SQLiteDatabase database;


    public PlayerDataSource(Context context)
    {
        this.context=context;
        helper=new PlayerOpenHelper(context);
    }
    public void open()
    {
        database=helper.getWritableDatabase();
        Log.i("data", "Database connection open");
    }
    public void close()
    {
        helper.close();
        Log.i("data", "Database connection close");

    }
}

here is the class PlayerOpenHelper that generates a database:

public class PlayerOpenHelper extends SQLiteOpenHelper {


public static final String DATABASENAME="player.db";
public static final String TABLE_PLAYER="tblplayer";    
public static final int DATABASEVERSION=1;

public static final String COLUMN_ID="id";
public static final String COLUMN_NAME="name";
public static final String COLUMN_DATE="date";
public static final String COLUMN_SCORE="score";
public static final String COLUMN_GAME="game";

private static final String CREATE_TABLE_PLAYER="CREATE TABLE IF NOT EXISTS " + 
        TABLE_PLAYER + "(" + COLUMN_ID +  " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " VARCHAR," + COLUMN_DATE + " VARCHAR,"
        + COLUMN_SCORE +" INTEGER," + COLUMN_GAME +   " VARCHAR "  +   ");";

SQLiteDatabase database;

public PlayerOpenHelper(Context context) {
    super(context, DATABASENAME, null, DATABASEVERSION);
    // TODO Auto-generated constructor stub
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_PLAYER);
    Log.i("data", "Table player has been just created");
}



@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYER);
    onCreate(db);

}

The problem is that when i try to do something with the database, to insert to it or to take data it makes an error that says that no such table:tblplayer

Omer
  • 307
  • 5
  • 17
  • possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – laalto Mar 08 '14 at 21:10
  • I can see the resemblance, but that for sure not the same. look at the accepted anser – Omer Mar 08 '14 at 21:15
  • Yes, the accepted answer has one way to solve the problem, given that you have implemented `onUpgrade()`. The linked dupe has more in-depth information. – laalto Mar 08 '14 at 21:17

2 Answers2

1

It should be

public static final String DATABASENAME="PLAYERS";

not

public static final String DATABASENAME="player.db";

.db extension will be given by Android itself.

PS. Remember to change the database version

Regards

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
1

A common source of error is the database version.

If you ran your code at least once without the 'CREATE TABLE' statement, and then you ran again with it, the table won't be created, because the system thinks that the database hasn't changed.

Try raising the version.

public static final int DATABASEVERSION=2; 
Merlevede
  • 8,140
  • 1
  • 24
  • 39