New to SQLite
and Android
development, my code worked until I added the "putDiaryInfo" method. According to the LogCat
my database leaked and I should close it in all methods when it's not being used, I tried to do this and it still doesn't seem to work.
Here is my code:
public class DatabaseOperations extends SQLiteOpenHelper {
public static int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE "+ TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT,"+TableData.TableInfo.USER_EMAIL+" TEXT,"+TableData.TableInfo.USER_WEIGHT+" INT,"+ TableData.TableInfo.USER_GOAL+" INT);";
public DatabaseOperations(Context context){
super(context, TableData.TableInfo.DATABASE_NAME,null,database_version);
Log.d("Database Operations", "Database Created");
}
@Override
public void onCreate(SQLiteDatabase sdb){
sdb.execSQL(CREATE_QUERY);
Log.d("Database Operations", "Database Created");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
public void putInformation(DatabaseOperations dop, String name, String pass, String email, int weight, int goal){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
cv.put(TableData.TableInfo.USER_WEIGHT, weight);
cv.put(TableData.TableInfo.USER_GOAL, goal);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME,null, cv);
Log.d("Database Operations", "One Raw Inserted");
SQ.close();
}
public void putDiaryInfo(DatabaseOperations dop, String email, int weight, int goal){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_WEIGHT, weight);
cv.put(TableData.TableInfo.USER_GOAL, goal);
long index = SQ.update(TableData.TableInfo.TABLE_NAME, cv, TableData.TableInfo.USER_EMAIL+"=?", new String[] {email});
SQ.close();
}
public Cursor getInformation(DatabaseOperations dop){
SQLiteDatabase SQ = dop.getReadableDatabase();
String[] columns = {TableData.TableInfo.USER_NAME, TableData.TableInfo.USER_PASS, TableData.TableInfo.USER_EMAIL, TableData.TableInfo.USER_WEIGHT, TableData.TableInfo.USER_GOAL};
Cursor CR = SQ.query(TableData.TableInfo.TABLE_NAME, columns, null, null, null, null, null);
SQ.close();
return CR;
}
}
Aucun commentaire:
Enregistrer un commentaire