本文介绍了删除Android的SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了sqlite的数据库命名为DGC.DB和表名是司机,现在我想将我现有的表中的SQLite数据库。我试图通过exute删除该删除查询,但其没有工作。我是新在android.how我可以做到这一点。

  ** ** DGCdb.java
           公共类PasswordDB{
    EditText上的EditText;
     TextView的电视;
    私有静态最后弦乐DATABASE_NAME =DGC.db;
    私有静态最终诠释DATABASE_VERSION = 1;
    静态最后弦乐TABLE_NAME =驱动程序;
    公共静态上下文的背景下;
    公共静态SQLiteDatabase分贝;    私人SQLiteStatement的insertstmt;    私有静态最后弦乐INSERT =插入+ TABLE_NAME + (mfname,msname,mun,mstreet,msuburb,mstate,mpost$c$c,mlicense,mid,mmobile,memail,mimagelicense,mauthcard,mpic)值(,,,,,,,,,,,,,??????????????);
    受保护的静态最后弦乐enternpw = NULL;
    公共PasswordDB(上下文的背景下){
    PasswordDB.context =背景;
    OpenHelper openHelper =新OpenHelper(PasswordDB.context);
    PasswordDB.db = openHelper.getWritableDatabase();
    this.insertStmt = PasswordDB.db.compileStatement(INSERT);
    }    众长插入(字符串mfname,字符串msname,字符串门,字符串mstreet,字符串msuburb,字符串mstate,字符串mpost code,字符串mlicense,字符串中旬,字符串mmobile,字符串memail,字节[] mimagelicense,字节[] mauthcard ,字节[] MPIC){
        this.insertStmt.bindString(1,mfname);
        this.insertStmt.bindString(2,msname);
        this.insertStmt.bindString(3门);
        this.insertStmt.bindString(4,mstreet);
        this.insertStmt.bindString(5,msuburb);
        this.insertStmt.bindString(6,mstate);
     // this.insertStmt.bindString(7,mpassword);
        this.insertStmt.bindString(7,mpost code);
        this.insertStmt.bindString(8,mlicense);
        this.insertStmt.bindString(9中旬);
        this.insertStmt.bindString(10,mmobile);
        this.insertStmt.bindString(11,memail);
        this.insertStmt.bindBlob(12,mimagelicense);
        this.insertStmt.bindBlob(13,mauthcard);
        this.insertStmt.bindBlob(14,MPIC);
        返回this.insertStmt.executeInsert();
    }
/ *公共无效deleteAll(){
    db.delete(TABLE_NAME,NULL,NULL);
    } * /
    公共无效DROPTABLE(){
          db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
        }    公开名单<的String []>全选()
    {
    清单<的String []>名单=新的ArrayList<的String []>();
   //光标指针= db.query(TABLE_NAME,新的String [] {FIRST_NAME,姓,unit_no,慨,郊区,国家, post_$c$c\",\"driving_lic\",\"authority_id\",\"mobile_no\",\"Email\",\"password\",\"license_pic\",\"audit_card_pic\",\"face_pic\",\" date_of_creation,状态,login_token,device_token,纬度,经度},NULL,NULL,NULL,NULL,mEmail ASC);
    光标光标= db.query(TABLE_NAME,新的String [] { \"id\",\"mfname\",\"msname\",\"mun\",\"mstreet\",\"msuburb\",\"mstate\",\"mpassword\",\"mconfirm_password\",\"mpost$c$c\",\"mlicense\",\"mid\",\"mmobile\",\"memail\",\"mimagelicense\",\"mauthcard\",\"mpic\" },NULL,NULL,NULL,NULL,memail ASC);
    //光标指针= db.query(TABLE_NAME,新的String [] { \"id\",\"first_name\",\"surname\",\"unit_no\",\"mstreet\",\"msuburb\",\"mstate\",\"mpassword\",\"mconfirm_password\",\"mpost$c$c\",\"mlicense\",\"mid\",\"mmobile\",\"memail\",\"mimagelicense\",\"mauthcard\",\"mpic\" },NULL,NULL,NULL,NULL,memail ASC);
    INT X = 0;
    如果(cursor.moveToFirst()){
       做{
        的String [] = B1新的String [] {cursor.getString(0),cursor.getString(1),cursor.getString(2)
                cursor.getString(3),cursor.getString(4),cursor.getString(5),
                cursor.getString(6),cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),cursor.getString(15)};
        list.add(B1);
        X = X + 1;
       }而(cursor.moveToNext());
    }
    如果(光标=空&放大器;!&放大器;!cursor.isClosed()){
       cursor.close();
    }
    cursor.close();
    返回列表;
   }
   公共无效删除(INT ROWID){
    db.delete(TABLE_NAME,NULL,NULL);
   }   私有静态类OpenHelper扩展SQLiteOpenHelper {
    OpenHelper(上下文的背景下){
         超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
    // db.execSQL(CREATE TABLE+ TABLE_NAME +(ID INTEGER PRIMARY KEY,FIRST_NAME TEXT,姓氏TEXT,unit_no TEXT,TEXT STREET_NAME,郊区文字,状态TEXT,pickup_time TEXT,date_of_creation文本,目标文本,文本组,票价TEXT,TEXT纬度,经度TEXT,城市TEXT,BLOB license_pic,audit_card_pic BLOB,face_pic BLOB));
        // db.execSQL(CREATE TABLE+ TABLE_NAME +(ID INTEGER PRIMARY KEY,mfname TEXT,msname TEXT,TEXT门,mstreet TEXT,msuburb TEXT,mstate TEXT,mpassword TEXT,mconfirm_password TEXT,mpost code文本,mlicense文本,文本中旬,mmobile TEXT,memail TEXT,BLOB mimagelicense,mauthcard BLOB,MPIC BLOB));
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
        {
         db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
         的onCreate(DB);
    }
   }
公共光标rawQuery(字符串selectQuery,Object对象){
    // TODO自动生成方法存根
    返回null;
}
公共静态PasswordDB getDBAdapterInstance(ViewData的viewPassword){
    // TODO自动生成方法存根
    返回null;
}
公共对象getenternpw(){
    // TODO自动生成方法存根
    返回null;
}
公共静态光标getSinlgeEntry(){
    // TODO自动生成方法存根    光标CUR = db.rawQuery(SELECT * FROM+ TABLE_NAME,NULL);
    // colName1 +=+名字+' - > wherclause
    的System.out.println(记录计数是+ cur.getCount());
    返回CUR;}公共静态无效的close(){
    // TODO自动生成方法存根}
公共PasswordDB的open(){
    // TODO自动生成方法存根
    返回null;
}公共无效删除(字符串tableName值,Object对象,对象Object2的){
    // TODO自动生成方法存根}公共光标查询(字符串tableName值,字符串[]字符串,Object对象,
        对象Object2的,对象object3,对象object4,串串){
    // TODO自动生成方法存根
    返回null;
}公共无效插入(FIRST_NAME字符串,字符串姓氏,字符串unit_no,
        STREET_NAME字符串,字符串郊区,字符串状态,字符串post_ code,
        字符串driving_lic,字符串authority_id,字符串mobile_no,
        字符串email,字符串密码,串license_pic,
        字符串audit_card_pic,字符串face_pic,字符串date_of_creation,
        字符串状态,字符串device_token,字符串纬度,经度字符串){
    // TODO自动生成方法存根}


解决方案

要删除一个表,使用:

替换

 公共无效deleteAll(){
    db.delete(TABLE_NAME,NULL,NULL);
 }

通过

 公共无效DROPTABLE(){
  db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
}

I have created a database in sqlite named as DGC.DB and table name is drivers and now I want to drop my existing table in sqlite database .I try to delete this by exute delete query but its not working .i am new in android.how can I do this.

      **DGCdb.java**
           public class PasswordDB

{
    EditText edittext;
     TextView tv;
    private static final  String DATABASE_NAME = "DGC.db";
    private static final int DATABASE_VERSION = 1;
    static final String TABLE_NAME = "Drivers";
    public static Context context;
    public static SQLiteDatabase db;

    private SQLiteStatement insertStmt;

    private static final String INSERT = "insert into " + TABLE_NAME + " (mfname,msname,mun,mstreet,msuburb,mstate,mpostcode,mlicense,mid,mmobile,memail,mimagelicense,mauthcard,mpic) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    protected static final String enternpw = null;
    public PasswordDB(Context context) {
    PasswordDB.context = context;
    OpenHelper openHelper = new OpenHelper(PasswordDB.context);
    PasswordDB.db = openHelper.getWritableDatabase();
    this.insertStmt = PasswordDB.db.compileStatement(INSERT);
    }



    public long insert(String mfname,String msname,String mun,String mstreet,String msuburb,String mstate,String mpostcode,String mlicense,String mid,String mmobile,String memail,byte[] mimagelicense,byte[] mauthcard,byte[] mpic ) {
        this.insertStmt.bindString(1, mfname);
        this.insertStmt.bindString(2, msname);
        this.insertStmt.bindString(3, mun);
        this.insertStmt.bindString(4, mstreet);
        this.insertStmt.bindString(5, msuburb);
        this.insertStmt.bindString(6, mstate);
     //   this.insertStmt.bindString(7, mpassword);
        this.insertStmt.bindString(7, mpostcode);
        this.insertStmt.bindString(8, mlicense);
        this.insertStmt.bindString(9, mid);
        this.insertStmt.bindString(10, mmobile);
        this.insertStmt.bindString(11, memail);
        this.insertStmt.bindBlob(12, mimagelicense);
        this.insertStmt.bindBlob(13, mauthcard);
        this.insertStmt.bindBlob(14, mpic);
        return this.insertStmt.executeInsert();
    }


/*    public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
    }*/
    public void dropTable() {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        }

    public List<String[]> selectAll()
    {
    List<String[]> list = new ArrayList<String[]>();
   // Cursor cursor = db.query(TABLE_NAME, new String[] { "first_name", "Surname","unit_no","Street_name","Suburb","State"," post_code","driving_lic","authority_id","mobile_no","Email","password","license_pic","audit_card_pic","face_pic"," date_of_creation","status","login_token","device_token","latitude", "longitude" }, null, null, null, null, "mEmail asc");
    Cursor cursor = db.query(TABLE_NAME, new String[] { "id","mfname","msname","mun","mstreet","msuburb","mstate","mpassword","mconfirm_password","mpostcode","mlicense","mid","mmobile","memail","mimagelicense","mauthcard","mpic" }, null, null, null, null, "memail asc");
    //Cursor cursor = db.query(TABLE_NAME, new String[] { "id","first_name","surname","unit_no","mstreet","msuburb","mstate","mpassword","mconfirm_password","mpostcode","mlicense","mid","mmobile","memail","mimagelicense","mauthcard","mpic" }, null, null, null, null, "memail asc");
    int x=0;
    if (cursor.moveToFirst()) {
       do {
        String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2),
                cursor.getString(3),cursor.getString(4),cursor.getString(5),
                cursor.getString(6),cursor.getString(7),cursor.getString(8),cursor.getString(9),cursor.getString(10),cursor.getString(11),cursor.getString(12),cursor.getString(13),cursor.getString(14),cursor.getString(15)};
        list.add(b1);
        x=x+1;
       } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
       cursor.close();
    }
    cursor.close();
    return list;
   }






   public void delete(int rowId) {
    db.delete(TABLE_NAME, null, null);
   }

   private static class OpenHelper extends SQLiteOpenHelper {
    OpenHelper(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
    //  db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY,first_name TEXT,Surname TEXT,unit_no TEXT,Street_name TEXT,Suburb TEXT,state TEXT,pickup_time TEXT,date_of_creation TEXT, destination TEXT, group TEXT,fare TEXT, latitude TEXT, longitude TEXT,city TEXT,license_pic BLOB,audit_card_pic BLOB,face_pic BLOB)");
        // db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, mfname TEXT, msname TEXT,mun TEXT,mstreet TEXT,msuburb TEXT,mstate TEXT,mpassword TEXT,mconfirm_password TEXT,mpostcode TEXT,mlicense TEXT,mid TEXT,mmobile TEXT,memail TEXT,mimagelicense BLOB,mauthcard BLOB,mpic BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
    }
   }


public Cursor rawQuery(String selectQuery, Object object) {
    // TODO Auto-generated method stub
    return null;
}
public static PasswordDB getDBAdapterInstance(ViewData viewPassword) {
    // TODO Auto-generated method stub
    return null;
}
public Object getenternpw() {
    // TODO Auto-generated method stub
    return null;
}
public static Cursor getSinlgeEntry() {
    // TODO Auto-generated method stub

    Cursor cur =db.rawQuery("SELECT * FROM "+  TABLE_NAME, null);
    // colName1+"='"+name+"'" ->  wherclause
    System.out.println("Record count are "+cur.getCount());
    return cur;

}

public static void close() {
    // TODO Auto-generated method stub

}
public PasswordDB open() {
    // TODO Auto-generated method stub
    return null;
}

public void delete(String tableName, Object object, Object object2) {
    // TODO Auto-generated method stub

}

public Cursor query(String tableName, String[] strings, Object object,
        Object object2, Object object3, Object object4, String string) {
    // TODO Auto-generated method stub
    return null;
}

public void insert(String first_name, String surname, String unit_no,
        String street_name, String suburb, String state, String post_code,
        String driving_lic, String authority_id, String mobile_no,
        String email, String password, String license_pic,
        String audit_card_pic, String face_pic, String date_of_creation,
        String status, String device_token, String latitude, String longitude) {
    // TODO Auto-generated method stub

}
解决方案

To Drop a table, use:

Replace

 public void deleteAll() {
    db.delete(TABLE_NAME, null, null);
 }

With

 public void dropTable() {
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
}

这篇关于删除Android的SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:24