-1

I am a rookie in databases. At the moment I have created an app and I want to link it to an SQLite Database. After following a tutorial in youtube, I have tested my database by trying to add a simple row. I get an error E/SQLiteLog: (1) no such table: SCORES E/SQLiteDatabase: Error inserting DScore=0 LScore=0 Name=Aris This is my code as of now:

     public class DB_Controller  {
    private DB_Scores helper;

    public DB_Controller(Context context) {
        helper = new DB_Scores(context);
    }

    public long insertRow(String name){
        SQLiteDatabase db=helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_Scores.NAME, name);
        values.put(DB_Scores.DSCORE, 0);
        values.put(DB_Scores.LSCORE, 0);
        long id = db.insert(DB_Scores.TABLE_NAME,null, values);
        return id;
    }


    static class DB_Scores extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "scores.db";
        private static final String TABLE_NAME = "SCORES";
        private static final int DATABASE_VERSION = 1;
        private static final String UID = "_id";
        private static final String NAME = "Name";
        private static final String LSCORE = "LScore";
        private static final String DSCORE = "DScore";
        private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY " +
                "AUTOINCREMENT, "+NAME+" TEXT UNIQUE, "
                +LSCORE+" INTEGER, "+DSCORE+" INTEGER);";
        private Context context;//?

        public DB_Scores(Context context ) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context = context;
            Toast.makeText(context, "Constructor was called", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(CREATE_TABLE);
                Toast.makeText(context, "onCreate was called", Toast.LENGTH_LONG).show();
            } catch (SQLException e) {
                Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try{
                db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
                Toast.makeText(context, "onUpgrade called", Toast.LENGTH_LONG).show();
                onCreate(db);
            }catch(SQLException e){
                Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
            }
        }
    }
}

And the activity that I create an object of DB_Controller
public class Scores_Activity extends AppCompatActivity {

private DB_Controller dbController;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scores_activity);      

    dbController = new DB_Controller(this);
    addRow();

}

public void addRow() {
    long id=dbController.insertRow("Aris");
    if (id < 0) {
        Toast.makeText(this, "Unsuccessful", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
    }
}

The lines that it marks as the error source are long id=dbController.insertRow("Aris");,
long id = db.insert(DB_Scores.TABLE_NAME,null, values);and addRow(); My guess is that I have made a logical error while creating the database.Thanks for your time

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

-2

change your create table command as follows (semicolon at the end of query is not required)

 private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY " +
            "AUTOINCREMENT, "+NAME+" TEXT UNIQUE, "
            +LSCORE+" INTEGER, "+DSCORE+" INTEGER )";
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Adeel Turk
  • 897
  • 8
  • 23
  • 1
    `semicolon at the end of query is not required` but it does not produce any harm if it's there. – Phantômaxx Apr 26 '17 at 09:13
  • what about database name i never seen .db any where i think you should give it a try with changing databsename to private static final String DATABASE_NAME = "scores"; and removing semicolons from query – Adeel Turk Apr 26 '17 at 09:18
  • The tutorial I've been watching claims that both are the same thing.@AdeelTurk – Aris Eleftheriadis Apr 26 '17 at 09:20
  • Where do you see a semicolon after `db` in `private static final String DATABASE_NAME = "scores.db";`? And... sorry to tell you, but nearly every database use the `.db` extension - by convention. Anyway, it's not that important. You COULD give it no extension at all as well as a fantasy extension like `.godzilla`, if so you wish. Android doesn't care about that. – Phantômaxx Apr 26 '17 at 09:20