2014-06-11 199 views
-2

我在sqlite中創建了一個名爲DGC.DB的數據庫,表名是驅動程序,現在我想在sqlite數據庫中刪除我現有的表。我試圖通過exute delete query刪除它,但它不是工作。我是新的android.how我可以這樣做。在android中刪除SQLite數據庫

 **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 

} 
+1

'我想放棄我現有的table'因此,使用'db.execSQL( 「DROP TABLE IF EXISTS」+ TABLE_NAME);'而不是'DELETE'(刪除內容,而不是表本身)。除此之外,你的刪除方法是**空**。 –

+0

順便說一句,你爲什麼要在刪除記錄後通過在方法'public void delete(int rowId)'中傳遞rowID來重新創建整個表? –

回答

0

刪除表,使用:

更換

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

隨着

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

我已經使用了QUERY U GIVEN,但桌子仍然保持原樣。 – user3725779

+0

您是否在執行drop table命令後重新創建表格? –

+0

我已經嘗試了你所說的一切......但表格仍然是在sqlite中,請檢查我的代碼告訴我錯在哪裏 – user3725779

相關問題