我从服务器获取JSON值并将数据保存到Android中的Sqlite数据库中,我使用SQLiteOpenHelper类创建数据库和表:
public class ActivityTableDBOpenHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "ATDBOpenHelper";
private static final String DATABASE_NAME = "MyDatabase.db";
private static final int DATABASE_VERSION = 1;
public static final String ACTIVITY_TABLE = "'activity'";
public static final String COLUMN_ACTIVITY_ID = "'activityId'";
public static final String COLUMN_USER_ID = "'userId'";
public static final String COLUMN_PROGRAM_ID = "'programId'";
public static final String COLUMN_ORDER = "'order'";
public static final String COLUMN_G_DATE = "'gDate'";
public static final String COLUMN_J_DATE = "'jDate'";
private static final String TABLE_CREATE =
"CREATE TABLE " + ACTIVITY_TABLE + " (" +
COLUMN_ACTIVITY_ID + " INTEGER(20), " +
COLUMN_USER_ID + " INTEGER(20), " +
COLUMN_PROGRAM_ID + " INTEGER(20), " +
COLUMN_ORDER + " VARCHAR(200), " +
COLUMN_G_DATE + " VARCHAR(200), " +
COLUMN_J_DATE + " VARCHAR(200) " +
")";
public ActivityTableDBOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i(LOGTAG, "Activity Table has been created!");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + ACTIVITY_TABLE);
onCreate(db);
}
}
我可以成功地将数据插入到活动表中,现在我想在用户单击按钮时删除所有记录,我使用此行代码并收到错误:
SQLiteDatabase database;
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
这是我的活动课程:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_table);
btnDeleteATData = (Button) findViewById(R.id.btnDeleteATData);
dataSource = new ActivityTableDataSource(this);
dataSource.open();
btnDeleteATData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
database=new SQLiteDatabase();
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
}
});
}
这是错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.delete(java.lang.String, java.lang.String, java.lang.String[])' on a null object reference
at com.rastari.salar.salytest.ActivityTableActivity$2.onClick(ActivityTableActivity.java:69)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
如何从活动表中删除所有记录?
谢谢。
答案 0 :(得分:1)
首先应该初始化SQLiteDatabase,所以转换它:
SQLiteDatabase database;
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
到此:
SQLiteDatabase database = new SQLiteDatabase(this); // or dbHelper.getWritableDatabase(); if you have a dbHelper
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
否则数据库对象为null并且您收到错误。 在这里,您可以找到所有操作的完整示例http://www.vogella.com/tutorials/AndroidSQLite/article.html
答案 1 :(得分:0)
db.execSQL(“从”删除“+ TABLE_NAME”;
答案 2 :(得分:0)
您的通话问题
short
是您的数据库对象为null。致电SQLiteDatabase database;
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE,null,null);
然后继续。
要从数据库或特定表中删除信息,您还可以使用基本的SQL查询:http://www.w3schools.com/sql/sql_delete.asp
SQLiteDatabase db = getWritableDatabase()
答案 3 :(得分:0)
//ourDb ->SqliteDatabase object
//TABLE_NAME ->
public void deleteDatafromAllTables() {
ourDb.execSQL("delete from " + TABLE_NAME);
}
答案 4 :(得分:0)
我发现我错了:D
感谢@Gabriella Angelova
我必须初始化SQLiteDatabase和SQLiteOpenHelper
public class ActivityTableActivity extends ActionBarActivity {
SQLiteDatabase database;
ActivityTableDBOpenHelper dbOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_table);
dbOpenHelper = new ActivityTableDBOpenHelper(this);
database = dbOpenHelper.getWritableDatabase();
btnDeleteATData = (Button) findViewById(R.id.btnDeleteATData);
dataSource = new ActivityTableDataSource(this);
dataSource.open();
btnDeleteATData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
database.delete(ActivityTableDBOpenHelper.ACTIVITY_TABLE, null, null);
}
});
}