How to update/add name and score in database
I m making a Quiz based app in Android. And I want to make Submit score
button at the last of the game if user want to do so. If user click on
that button it will ask to Enter the name and then the name with the score
will be saved in the database. So, now my question is how to perform this
task. I have managed a Separate Database class for all database Queries.
So, please tell me according to this situation..
Here is my Database Class which I have created till now..
public class DBAdapter
{
public static final String KEY_id = "id";
public static final String KEY_ques = "ques";
public static final String KEY_correctans = "correctans";
public static final String KEY_wrongans1 = "wrongans1";
public static final String KEY_wrongans2 = "wrongans2";
public static final String KEY_wrongans3 = "wrongans3";
public static final String KEY_scoreid = "scoreid";
public static final String KEY_name = "name";
public static final String KEY_score = "score";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "quiz";
private static final String DATABASE_TABLE = "quiz_ques";
private static final String DATABASE_TABLE1 = "scores";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table quiz_ques (id
integer primary key autoincrement,"
+ "ques TEXT " + "correctans TEXT " + "wrongans1 TEXT" +
"wrongans2 TEXT" + "wrongans3 TEXT"
+ ");";
private static final String DATABASE_CREATE1 = "create table scores
(scoreid integer primary key autoincrement,"
+ "name TEXT" + "score NUMERIC"
+ ");";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try {
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE1);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS quiz_ques");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close()
{
DBHelper.close();
}
public Cursor getText(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
KEY_id,
KEY_ques,KEY_correctans,KEY_wrongans1,KEY_wrongans2,KEY_wrongans3
}, KEY_id + "="
+ rowId, null,null,null, null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor addscore(long rowid) throws SQLException
{
**//What to write here..**
}
}
No comments:
Post a Comment