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();
}
}
}
TABLE_NAME_Hardtwice, change the 1st statement toTABLE_NAME_Easy