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.