我想在允许擦除表中所有数据的方法中执行SQL查询。我的解决方案有效,但我认为不是很干净,陪审团会很感激。
现在,我在参数中调用了一个带日期的方法,但我不需要使用它。这是我找到的唯一解决方案。当我尝试放置一个void时,逻辑上我不能使用这个使用return的方法。
public long Effacer(String date) {
return ourDatabase.delete(DATABASE_TABLE, null, null);
}
嗯,对你来说,删除我的桌子并重新创建它的方式越干净。我创造了一种方法而不是DbHelper,但我不知道我怎么能打电话给她。我相信这是更干净的方式,但我只是一个初学者,所以我不确定。你在想什么?
(我已经在代码中进行了任何测试) 这是我的代码:
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(仅当我们点击删除按钮时才有部分)
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();
}
答案 0 :(得分:1)
我不知道为什么有些人会对我产生负面评价,我们不能说我没有做任何努力,我们可以在评论中看到我的任何测试。此外,我有一个解决方案,适用于我的问题,如果我没有搜索,我将永远不会找到这种方式。问题只是这样做的方式以及我想要干净的事实,所以我们可以说我的帖子只是出于对计算的兴趣而创建。
好吧,因为我不喜欢那些在没有帮助的情况下批评的人,我会给你解决方案,因为我亲自想到了另一个。 这是我公司的一个人找到的解决方案。在类DbHelper中调用onDelete方法的解决方案是:
DbHelper dbHelper = new DbHelper(arg0.getContext());
dbHelper.onDelete();
关心,