2013-12-19 42 views
0

我是SQLite数据库和Android应用程序的新手。我想在用户输入名称时删除特定记录,然后单击“删除帐户”按钮。从SQLite中的数据库中删除一行Android

我有以下代码,但我似乎无法实现我想要的。你对我的代码有什么问题有什么看法吗?

  • mydbadapter.java
  • private static final String DATABASE_NAME = "customer.db"; 
        private static final String DATABASE_TABLE = "customerAcc"; 
        private static final int DATABASE_VERSION = 1; 
        private SQLiteDatabase _db; 
        private final Context context; 
    
        public static final String name = "_name"; 
        public static final int COLUMN_NAME_ID = 0; 
        public static final String password = "_password"; 
        public static final int COLUMN_PW_ID = 1; 
        public static final String address = "_address"; 
        public static final int COLUMN_ADDRESS_ID = 2; 
        public static final String phoneNumber = "_phoneNumber"; 
        public static final int COLUMN_PHONE_ID = 3; 
    
    
        protected static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " " + "(" + name + " Text not null, " + password + " Text not null, " 
          + address + " Text not null, " + phoneNumber + " text not null);"; 
    
        private String MYDBADAPTER_LOG_CAT = "MY_LOG"; 
    
        private MyDBOpenHelper dbHelper; 
    
        public MyDbAdapter(Context _context) 
        { 
         this.context = _context; 
    
         dbHelper = new MyDBOpenHelper (context, DATABASE_NAME, null, DATABASE_VERSION); 
        } 
    
        public void close() 
        { 
         _db.close(); 
         Log.w(MYDBADAPTER_LOG_CAT, "DB Closed"); 
        } 
    
        public void open() throws SQLiteException 
        { 
         try 
         { 
          _db = dbHelper.getWritableDatabase(); 
          Log.w(MYDBADAPTER_LOG_CAT, "DB opened as writable database"); 
         } 
         catch (SQLiteException e) 
         { 
          _db = dbHelper.getReadableDatabase(); 
          Log.w(MYDBADAPTER_LOG_CAT, "DB opened as readable database"); 
         } 
        } 
    
        public long insertEntry(String cname, String cpassword, String caddress, String cphone) 
        { 
         // create codes go here 
        } 
    
        public boolean removeEntry(String custname) 
        { 
         try 
         { 
         _db = dbHelper.getWritableDatabase(); 
    
         return _db.delete(DATABASE_TABLE, name + "=" + custname, null)>0; 
         } 
         catch (SQLiteException e) 
         { 
          return false; 
         } 
        } 
    
        public boolean updateEntry(long _rowIndex, String entryName, String entryTel) 
        { 
         //update codes are here 
        } 
    
        public Cursor retrieveAllEntriesCursor() 
        { 
         //retrieve codes are here 
        } 
    
        public class MyDBOpenHelper extends SQLiteOpenHelper 
        { 
         public MyDBOpenHelper(Context context, String name, CursorFactory factory, int version) 
         { 
          super(context, name, factory, version); 
          // TODO Auto-generated constructor stub 
         } 
    
         @Override 
         public void onCreate(SQLiteDatabase db) 
         { 
          // TODO Auto-generated method stub 
          db.execSQL(DATABASE_CREATE); 
          Log.w(MYDBADAPTER_LOG_CAT, "Helper : DB " + DATABASE_TABLE + " Created!!"); 
         } 
    
    
        } // End of myDBOpenHelper 
    
    }// End of myDBAdapter 
    

  • MainActivity.java
  • btnRemove.setOnClickListener(new OnClickListener() 
        { 
    
         @Override 
         public void onClick(View arg0) { 
          // TODO Auto-generated method stub 
          String custname = etRName.getText().toString(); 
    
          myDB.removeEntry(custname); 
         }  
        }); 
    

    我的代码不给我任何错误,但是当我点击 '删除账户' 按钮成绩仍然是那里。

    +0

    你可以发布从logcat崩溃日志? – Andros

    +0

    让我看看你的DBHelper或Database类。 –

    +0

    @ManishPatel,我已经更新了我的问题。谢谢! OMG! –

    回答

    1

    这一工程,但我会建议

    db.delete(TABLE, "column_name=?", new String[] { String.valueOf(custname) }); 
    

    或使用

    String query = "DELETE FROM " +TABLE_NAME+ " WHERE " + COLUM_NAME+ " = " + "'"+VALUE +"'" ; 
    db.execSQL(query); 
    
    +0

    谢谢!这个答案就像一个魅力!我的天啊!谢谢! –

    +0

    这是我的荣幸。 –

    0

    公共布尔removeEntry(字符串CUSTNAME) {

    bolean returnBoolean;

    尝试 {

    db.delete(TABLE_NAME, "column_name=?", new String[]{String.valueOf(custname)}); 
    returnBoolean= true; 
    

    }

    catch(Exception e) 
    { 
        returnBoolean= false; 
        e.printStackTrace(); 
    } 
    
    finally 
    { 
        db.close(); 
    } 
    

    回报returnBoolean; }

    0

    您的查询语法看起来有些问题。这里是我的代码行,它适用于我

    db.delete(Table_name, KEY_COLUMN + "=?", new String[] {String.valueOf(custname)}); 
    

    你没有得到的查询结构。第二个参数是,其中子句的查询。看到THIS后的第一个答案,以更好地理解语法。

    0

    可能我建议采取看看cupboard,在这里你的代码将被大大减少/简化,它使这种的工作如此多可读性/可理解:)