android - How to delete all Table records in Sqlite? -
i json value server , save data database in sqlite in android, create database , table sqliteopenhelper class:
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); }
}
i can insert data activity table, want delete records when user click button, use line code , error:
sqlitedatabase database; database.delete(activitytabledbopenhelper.activity_table,null,null);
this activity class:
@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); } }); }
this error:
java.lang.nullpointerexception: attempt invoke virtual method 'int android.database.sqlite.sqlitedatabase.delete(java.lang.string, java.lang.string, java.lang.string[])' on null object reference @ com.rastari.salar.salytest.activitytableactivity$2.onclick(activitytableactivity.java:69) @ android.view.view.performclick(view.java:4780) @ android.view.view$performclick.run(view.java:19866) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
how can delete records activity table?
thank you.
you should first initialize sqlitedatabase, convert this:
sqlitedatabase database; database.delete(activitytabledbopenhelper.activity_table,null,null);
to this:
sqlitedatabase database = new sqlitedatabase(this); // or dbhelper.getwritabledatabase(); if have dbhelper database.delete(activitytabledbopenhelper.activity_table,null,null);
else database object null , error. here find whole example operations http://www.vogella.com/tutorials/androidsqlite/article.html
Comments
Post a Comment