-1

I'm trying to make database for my program and I'm having a lot of dumb problems... It's fragment of main activity:

Database db = new Database(this,editText.getText().toString());
String text = db.printRow();
textView.setText(text);

Now database class:

String nickname="EmptyNick";
public Database(Context context, String name) {
    super(context, "database.db", null, 1);
    nickname = name;
}

public void onCreate(SQLiteDatabase db) {
    if(!nickname.equals("EmptyNick")) {
        db.execSQL("create table player(id integer primary key autoincrement,nick text);");
        Users user = new Users();
        user.setNick("Mariusz");
        addPlayer(user);
    }
    else {
        //not important
    }
}

private void addPlayer(Users user) {
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("nick",user.getNick());
    db.insertOrThrow("player",null,values);
}

public String printRow() {
    String string=null;
    if(!nickname.equals("EmptyNick")) {
            String[] collumns = {"id","nick"};
            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.query("player",collumns,null,null,null,null,null);
            cursor.moveToFirst();
            while (cursor.moveToNext()) {
                string += cursor.getString(1);
            }
    }
    else {
        //not important
    }
    return string;
}

Errors: no such table: player Caused by: java.lang.reflect.InvocationTargetException Caused by: android.database.sqlite.SQLiteException: no such table: player (code 1): , while compiling: SELECT id, nick FROM player

I really can't see what's wrong. Error says there is no table 'player', but it is. On the beggining of onCreate methon in line:

db.execSQL("create table player(id integer primary key autoincrement,nick text);");

Can somebody help me? If I make Toast instead of text.setText(...) it shows me empty field, so yes, it can't create specific row. I understand the error, but do not from where and why it comes.

Charliee
  • 13
  • 6
  • `onCreate` is called by the super constructor. `name` is not updated at this point, therefore you don't call the create query, for some reason. – njzk2 Dec 11 '14 at 15:49
  • Der Golem nickName is NOT "EmptyNick", because of this: Database db = new Database(this,editText.getText().toString()); I put to constructor also string from editText. But if it's true, that onCreate is called by super() (as njzk2 says) this may be the answer. I'll check it in a minute. – Charliee Dec 11 '14 at 16:12
  • Well, even if I deleted all of lines about conditions the same error still exists... I really can't get this. In theory everything works, in practice buum, no such table goodbye :/ – Charliee Dec 11 '14 at 16:38
  • Did you read [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run)? – CL. Dec 11 '14 at 17:45

2 Answers2

0

The table you try to use does not exist. In such cases it is generally a good idea to check the database you are using and check whether the table exists. In this particular case it seems that you create the table if a condition is met. The condition is that your nickname's value differs from the default value. However, it does not differ. Make sure that you use a table if and only if it exists and you have the correct condition for creating the table.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

I don't really understand your problem. If there is an error, it will throw an ANR message, how can you print the toast and it shows the empty? and the Toast showed empty because you add only one record of data to your table, then in your printRow(), you called cursor.moveToFirst, then you call while(cursor.moveToNext()) which will move your cursor to second row and it will return false since you only have 1 record.

Dara
  • 107
  • 1
  • 7