-
-
Save vivdub/703b0dd9d6c378f14508 to your computer and use it in GitHub Desktop.
Getting error with code............
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//======================================================================================= | |
public class GameDB { | |
private static String _DB_NAME = "GAME_FRAMEWORK.db"; | |
private static int _DB_VERSION =1; | |
private Context _context; | |
private DBHelper _helper; | |
//===================================================== | |
public GameDB(Context context){ | |
this._context = context; | |
this._helper = new DBHelper(this._context, this._DB_NAME, null, this._DB_VERSION); | |
this._helper = new DBHelper(this._context); | |
SQLiteDatabase sqdb = this._helper.getWritableDatabase(); | |
sqdb.close(); | |
} | |
//===================================================== | |
/**This will save the user game, loss, of a particular game across a session, if record already exists for | |
* same session and same game, it will be updated( wins and losses added after the present ones ) */ | |
public void saveGameRecord(String session_id, GAME game,int win, int loss){ | |
try{ | |
if(recordExistsForThisSessionAndGame(session_id, game)) | |
updateUserRecordForThisSession(session_id, game, win, loss); | |
else | |
insertUserGameRecord(session_id, game, win, loss); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
//===================================================== | |
private boolean recordExistsForThisSessionAndGame(String session_id, GAME game){ | |
try{ | |
SQLiteDatabase sqdb; | |
sqdb = this._context.openOrCreateDatabase(this._DB_NAME, this._context.MODE_PRIVATE, null); | |
String query= " select * from "+TABLE.GAME_RECORD+" where "+GAME_RECORD.SESSION_ID+"="+session_id+" and "+GAME_RECORD.GAME_ID+"="+game+" "; | |
Cursor c=sqdb.rawQuery(query, null); | |
int count=0; | |
if(!c.moveToFirst()) | |
while(!c.isAfterLast()) | |
count=1; | |
c.close(); sqdb.close(); | |
if(count>0) return true; | |
}catch(Exception e){e.printStackTrace();} | |
return false; | |
} | |
//===================================================== | |
private void updateUserRecordForThisSession(String session_id, GAME game, int win, int loss){ | |
try{ | |
SQLiteDatabase sqdb= this._context.openOrCreateDatabase(this._DB_NAME, this._context.MODE_PRIVATE, null); | |
String query; | |
query = " update "+TABLE.GAME_RECORD+" set "+GAME_RECORD.LOSS+" = ("+GAME_RECORD.LOSS+"+"+loss+"), "+GAME_RECORD.WIN+" = ("+GAME_RECORD.WIN+"+"+win+") where "+GAME_RECORD.SESSION_ID+" = "+session_id+" and "+GAME_RECORD.GAME_ID+" = "+game+" ; "; | |
sqdb.execSQL(query); | |
sqdb.close(); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
//===================================================== | |
private void insertUserGameRecord(String session_id, GAME game, int win, int loss){ | |
try{ | |
SQLiteDatabase sqdb= this._context.openOrCreateDatabase(this._DB_NAME, this._context.MODE_PRIVATE, null); | |
String query = " insert into "+TABLE.GAME_RECORD+" ("+GAME_RECORD.SESSION_ID+", "+GAME_RECORD.GAME_ID+", "+GAME_RECORD.WIN+", "+GAME_RECORD.LOSS+") values ("+session_id+", "+game+", "+win+", "+loss+") "; | |
sqdb.execSQL(query); | |
sqdb.close(); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
private static class DBHelper extends SQLiteOpenHelper{ | |
//=================================================== | |
public DBHelper(Context context){ | |
super(context, GameDB._DB_NAME, null, GameDB._DB_VERSION); | |
Log.i("message","inside helper constructor"); | |
} | |
//=================================================== | |
public DBHelper(Context context, String name, CursorFactory factory,int version) { | |
super(context, name, factory, version); | |
} | |
//=================================================== | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
Log.i("message","inside oncreate"); | |
//createTables(db); | |
} | |
//==================================================== | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
} | |
//==================================================== | |
/**This creates following tables:<br> | |
* <ul> | |
* <li> | |
* Game Record : fields (record_id, session_id, game_id, win, loss) | |
* </li> | |
*</ul> */ | |
private void createTables(SQLiteDatabase db){ | |
try{ | |
String query; | |
query = "Create table if not exists "+TABLE.GAME_RECORD+" ("+GAME_RECORD.RECORD_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+GAME_RECORD.GAME_ID+" varchar(50), "+GAME_RECORD.WIN+" int(15), "+GAME_RECORD.LOSS+" int(15)) "; | |
db.execSQL(query); | |
}catch(Exception e){e.printStackTrace();} | |
} | |
} | |
//======================================================================================= | |
class TABLE{ | |
public static final String GAME_RECORD = "game_record"; | |
} | |
//======================================================================================= | |
class GAME_RECORD{ | |
public static final String RECORD_ID = "RECORD_ID"; | |
public static final String GAME_ID = "GAME_ID"; | |
public static final String SESSION_ID = "SESSION_ID"; | |
public static final String WIN = "WIN"; | |
public static final String LOSS = "LOSS"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment