插入一个按钮,允许我删除Android上的所有SQLite表信息
遵循本教程 我也成功地从SQLite数据库中获取信息并显示在列表视图中 然而,我不知道如何制作一个按钮,在插入时删除这些信息,以及这些代码应该放在哪里 如果有人能帮忙,我将不胜感激 这是我的dboperations.java 公共类DbOperations扩展了SQLiteOpenHelper{插入一个按钮,允许我删除Android上的所有SQLite表信息,android,sql,sqlite,button,Android,Sql,Sqlite,Button,遵循本教程 我也成功地从SQLite数据库中获取信息并显示在列表视图中 然而,我不知道如何制作一个按钮,在插入时删除这些信息,以及这些代码应该放在哪里 如果有人能帮忙,我将不胜感激 这是我的dboperations.java 公共类DbOperations扩展了SQLiteOpenHelper{ private static final int DB_VERSION = 1; private static final String DB_NAME = "standing_info.db"; pr
private static final int DB_VERSION = 1;
private static final String DB_NAME = "standing_info.db";
private static final String TABLE_NAME = "standing_table";
private static final String CREATE_QUERY = "create table "+StandingContract.StandingEntry.TABLE_NAME+
"("+ StandingContract.StandingEntry.ID+ " text,"+ StandingContract.StandingEntry.WON+" text,"+
StandingContract.StandingEntry.HALVED+ " text,"+ StandingContract.StandingEntry.LOST+" text,"+
StandingContract.StandingEntry.POINTS+ " text);";
DbOperations(Context ctx) {
super(ctx, DB_NAME, null, DB_VERSION);
Log.d("Database operations", "Database created...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table created...");
}
public void addInformations ( SQLiteDatabase db,String id,String won, String halved, String lost, String points){
ContentValues contentValues = new ContentValues();
contentValues.put(StandingContract.StandingEntry.ID,id);
contentValues.put(StandingContract.StandingEntry.WON,won);
contentValues.put(StandingContract.StandingEntry.HALVED,halved);
contentValues.put(StandingContract.StandingEntry.LOST,lost);
contentValues.put(StandingContract.StandingEntry.POINTS,points);
db.insert(StandingContract.StandingEntry.TABLE_NAME, null, contentValues);
Log.d("Database operations", "One Row Inserted...");
}
public Cursor getInformations(SQLiteDatabase db)
{
String[] projections = {StandingContract.StandingEntry.ID, StandingContract.StandingEntry.WON,
StandingContract.StandingEntry.HALVED, StandingContract.StandingEntry.LOST, StandingContract.StandingEntry.POINTS};
Cursor cursor = db.query(StandingContract.StandingEntry.TABLE_NAME,projections,
null,null,null,null,null,null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
在你的主要活动中
DatabaseAdapter.DatabaseHelper db = new DatabaseAdapter.DatabaseHelper(getBaseContext());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db.deleteAll();
}
});
在您的DatabaseAdapter中,它是您案例中的dboperations
public boolean deleteAll() {
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TBL_TABLE1);
db.execSQL("DROP TABLE IF EXISTS " + TBL_TABLE2);
return true;
}
请描述场景..而不是给出YouTube上的链接这里是我尝试删除的内容。setOnClickListener(new OnClickListener(){@Override public void onClick(View v){}public void DeleteDatabase(SQLiteDatabase db,String table){db.execSQL(“如果存在DROP table”+“standing_table”);db.close();};我的主要问题是,如果我要添加一个按钮来删除我制作的sqlite表,我是否需要使用除按钮所在的xml和My MainActivity.java以外的任何东西。向我们展示您的尝试。共享您的代码。我是否需要创建一个除My MainActivity之外的新java类,以便在单击按钮时启动活动?您的activity应该扩展SQLiteOpenHelper并实现其方法。在dboperations.java中,使用我提供的代码。您可以从另一个活动调用此方法。您很可能是正确的,但我需要一个要点,一步一步地说明如何放置位置和放置内容…public void addStanding(View View View){Intent i=new Intent(MainActivity.this,SaveInfo.class);startActivity(i);}公共无效显示列表(视图){Intent i=newintent(MainActivity.this,displayStandings.class);startActivity(i);}
public class MainActivity extends AppCompatActivity {
Button Login, Register, Delete;
int status = 0;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Login = (Button) findViewById(R.id.Login);
Register = (Button) findViewById(R.id.Reg);
Delete = (Button) findViewById(R.id.buttondelete);
Register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(i);
}
});
Delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
public void DeleteDatabase(SQLiteDatabase db, String table) {
db.execSQL("DROP TABLE IF EXISTS " + "standing_table");
db.close();
}