Reputation:
I'm having hard time trying to create Three tables and insert values to them.
The SQlite queries are correct since it worked with only one table before i have tried to make three tables and insert values to them.
Also,i put only part of GameOver class and i declared the Datatypes. Thanks for the help.
import static com.avrahamzilberblat.battleshipfinal.Constants.COL1;
import static com.avrahamzilberblat.battleshipfinal.Constants.COL2;
import static com.avrahamzilberblat.battleshipfinal.Constants.COL3;
import static com.avrahamzilberblat.battleshipfinal.Constants.TABLE_NAME_Easy;
import static com.avrahamzilberblat.battleshipfinal.Constants.TABLE_NAME_Hard;
import static com.avrahamzilberblat.battleshipfinal.Constants.TABLE_NAME_Normal;
import static com.avrahamzilberblat.battleshipfinal.Constants.TAG;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context,String tableName) {
super(context, tableName, null, 5);
SQLiteDatabase dbEasy=this.getWritableDatabase();
SQLiteDatabase dbNormal=this.getWritableDatabase();
SQLiteDatabase dbHard=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME_Hard + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
db.execSQL("CREATE TABLE " + TABLE_NAME_Normal + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
db.execSQL("CREATE TABLE " + TABLE_NAME_Hard + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME_Easy);
onCreate(db);
}
public boolean addData(String tableName,PlayerDetails playerDetails) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2,playerDetails.getWinnerName());
contentValues.put(COL3, playerDetails.getRatio());
long result = db.insert(tableName, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public class GameOver extends AppCompatActivity {
public void addData(String tableName,PlayerDetails playerDetails)
{
mDatabasehelper=new DatabaseHelper(this,tableName);
boolean insertData=mDatabasehelper.addData(tableName,playerDetails);
if(insertData)
{
Toast.makeText(this, "Data Successfully added", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Something Went Wrong ", Toast.LENGTH_SHORT).show();
}
}
}
Upvotes: 0
Views: 33
Reputation: 56953
You are trying to create the same table (_Hard) a second time. It would appear that you want to create _Easy. The following change is what I believe you want :-
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME_Easy + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
db.execSQL("CREATE TABLE " + TABLE_NAME_Normal + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
db.execSQL("CREATE TABLE " + TABLE_NAME_Hard + " (ID INTEGER PRIMARY KEY, " +
COL2 +" TEXT,"+COL3+" REAL)");
}
Note that after making the changes you will need to do one of the following before rerunning the App :-
Uninstall the App.
P.S. it would be simpler/better to have an indicator for the difficulty (Easy, Normal, Hard) and thus a single table rather than three (allows you to more easily say select all bu easy e.g. SELECT * FROM the_one_table WHERE difficulty = 'normal' OR difficulty = 'hard'
)
Upvotes: 1