I want to execute a SQL query in a method which permit to erase all data in my table. My solution work but I don’t believe that very clean and the jury will appreciate.
Right now, I call a method with date in parameter but I don’t need to use it. It’s the only solution that I have found. When I try to put a void, logically I can’t use this method which uses return.
public long Effacer(String date) {
return ourDatabase.delete(DATABASE_TABLE, null, null);
}
Well for you what the way is the more clean for drop my table and recreate it. I had create a method une DbHelper but i don’t know after how i can call her. I believe that’s the way the more clean but i’m just a beginner so i’m not sure. What are you thinking ?
(i have let any of my tests in the code)
Here is my code :
HotOrNot.java
public class HotOrNot
{
public static final String KEY_ID = "id_operation";
public static final String KEY_MONTANT = "montant";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_DATE = "date";
private static final String DATABASE_NAME = "MyBudget";
private static final String DATABASE_TABLE = "Operations";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private static SQLiteDatabase ourDatabase;
public static class DbHelper extends SQLiteOpenHelper
{
// Constructeur
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
+ " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT NOT NULL);");
}
/* @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) */
public void onUpgrade(SQLiteDatabase ourDatabase)
{
ourDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(ourDatabase);
}
public void onDelete()
{
ourDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
ourDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
+ " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
}
// constructeur
public HotOrNot(Context c)
{
ourContext = c;
}
public HotOrNot open() throws SQLException
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
/*ourDatabase.execSQL("DROP TABLE " + DATABASE_TABLE);
ourDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
+ " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT NOT NULL);");
*/
//ourDatabase = ourHelper.
//ourDatabase = ourHelper.onUpgrade(ourDatabase);*/
return this;
}
public void close()
{
ourHelper.close();
}
public long createEntry(String date, String montant, String description)
{
ContentValues cv = new ContentValues();
cv.put(KEY_DATE, date);
cv.put(KEY_MONTANT, montant);
cv.put(KEY_DESCRIPTION, description);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData()
{
String[] columns = new String[]
{ KEY_DATE, KEY_MONTANT, KEY_DESCRIPTION };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
String result = "";
int iDate = c.getColumnIndex(KEY_DATE);
int iMontant = c.getColumnIndex(KEY_MONTANT);
int iDescription = c.getColumnIndex(KEY_DESCRIPTION);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = result + c.getString(iDate) + " " + c.getString(iMontant)
+ " " + c.getString(iDescription) + "\n";
}
return result;
}
public String getSolde()
{
// Création de notre requete et alias
String query = "SUM(id_operation) AS " + KEY_ID;
String[] otherColumns = new String[]{ query };
Cursor cursorBidon = ourDatabase.query(DATABASE_TABLE, otherColumns, null, null, null, null, null);
cursorBidon.moveToFirst();
int iOperation = cursorBidon.getColumnIndex(KEY_ID);
String monSolde = "";
monSolde = monSolde + cursorBidon.getString(iOperation);
return monSolde;
}
/*
public void Erase(SQLiteDatabase db)
{
db.execSQL("DROP TABLE " + DATABASE_TABLE);
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_DATE
+ " TEXT NOT NULL, " + KEY_MONTANT + " TEXT NOT NULL, "
+ KEY_DESCRIPTION + " TEXT NOT NULL);");
}*/
public long Erase(String date)
{
return ourDatabase.delete(DATABASE_TABLE, null, null);
}
}
SQLiteExample.java (only part when we click on delete button)
case R.id.bSQLDelete:
boolean samarche = true;
try
{
String date = sqlDate.getText().toString();
HotOrNot entryyy = new HotOrNot(SQLiteExample.this);
entryyy.open();
//entryyy.Effacer(date);
entryyy.close();
}
I don’t know why some people have judged me negatively, we cannot say i have do nothing efforts, we can see any of my test in comment. Moreover i’ve a solution which works for my problem, if i had not searched i would never found this way. The problem was just the way to do it and the fact i would like anything of clean, so we can say my post was only created by interest in computing.
Well, because i’m not like people which critize without help i give you the solution because personally i think to the other.
This is solution has been found by one people in my company. The solution for call the method onDelete in the class DbHelper is :
With Regards,