2013-01-16 44 views
0

我正在開發一個具有14-15個表的數據庫的Android應用程序。我想訪問這些表格顯然用於查詢,插入,刪除等。我應該如何在Android應用程序中構建數據庫訪問方法?

我應該做一個單一的幫手,打開數據庫,然後創建一個單一的適配器,將訪問它並查詢它,因爲我打算?或者爲每個桌子幫助一下?

我對這一點感到困惑。第一個選項對我來說似乎更現實,因爲我不需要多次打開數據庫。

回答

1

創建一個從SQLiteOpenHelper開始的類,併爲每個類創建方法。 一個用於插入,一個用於刪除,一個用於更新,另一個用於檢索ResultSet。 下面是一個例子:

public class Db extends SQLiteOpenHelper { 
    static final int version=1; 
    static final String dbName="phoneBook"; 
    static final String dbtable="data"; 
    Context cont; 

    public Db(Context context) 
    { 
     super(context, dbName, null, version); 
     cont = context; 
    } 

    public void onCreate(SQLiteDatabase db) 
    { 
     try { 
      String sql = "CREATE TABLE "+dbtable+" (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, phone TEXT)"; 
      db.execSQL(sql); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void addContact(String name, String number) 
    { 
     try 
     { 
      SQLiteDatabase db = getWritableDatabase(); 
      String sql = "INSERT INTO "+dbtable+" VALUES(NULL,'"+name+"', '"+number+"')"; 
      db.execSQL(sql); 
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
    public void updateContact(int sid, String name, String number) { 
     try { 
      SQLiteDatabase db = getWritableDatabase(); 
      String sql = "UPDATE "+dbtable+" SET name = '"+name+"', phone = '"+number+"' WHERE id = "+sid; 
      db.execSQL(sql); 
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 
    public ArrayList<String> getContactByPhone(String number) { 
     try { 
      SQLiteDatabase db=this.getReadableDatabase(); 
      ArrayList<String> al = new ArrayList<String>(); 
      Cursor cur = db.rawQuery("SELECT * from "+dbtable+" WHERE phone like '%"+number+"%'",null); 
      while(cur.moveToNext()) 
       al.add(cur.getInt(0)+","+cur.getString(1)+","+cur.getString(2)); 
      db.close(); 
      return al; 
     } catch(SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

    public ArrayList<String> getContactByName(String sname) { 
     try { 
      SQLiteDatabase db=this.getReadableDatabase(); 
      ArrayList<String> al = new ArrayList<String>(); 
      Cursor cur = db.rawQuery("SELECT * from "+dbtable+" WHERE name like '%"+sname+"%'",null); 
      while(cur.moveToNext()) 
       al.add(cur.getInt(0)+","+cur.getString(1)+","+cur.getString(2)); 
      db.close(); 
      return al; 
     } catch(SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

    public ArrayList<String> getAllContact() { 
     try { 
      SQLiteDatabase db=this.getReadableDatabase(); 
      ArrayList<String> al = new ArrayList<String>(); 
      Cursor cur = db.rawQuery("SELECT * from "+dbtable,null); 
      while(cur.moveToNext()) 
       al.add(cur.getInt(0)+","+cur.getString(1)+","+cur.getString(2)); 
      db.close(); 
      return al; 
     } catch(SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return null; 
    } 

    public void deleteContact(int sid) { 
     try { 
      SQLiteDatabase db = getWritableDatabase(); 
      String sql = "DELETE FROM "+dbtable+" WHERE id = "+sid; 
      db.execSQL(sql); 
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void deleteData() { 
     try { 
      SQLiteDatabase db = getWritableDatabase(); 
      db.execSQL("DELETE FROM "+dbtable); 
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void showAll() { 
     try { 
      SQLiteDatabase db = getReadableDatabase(); 
      Cursor cur = db.rawQuery("SELECT * from "+dbtable, null); 
      while(cur.moveToNext()) 
       Toast.makeText(cont, cur.getString(1)+" "+cur.getString(2), Toast.LENGTH_LONG).show(); 
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    public int getCount() { 
     int total = 0; 
     try { 
      SQLiteDatabase db = getReadableDatabase(); 
      Cursor curs = db.rawQuery("SELECT * FROM "+dbtable+" WHERE 1",null); 
      total = curs.getCount();    
      db.close(); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return total; 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     try { 
      db.execSQL("DROP TABLE IF EXISTS "+dbtable); 
      onCreate(db); 
     } catch (SQLException e) { 
      Toast.makeText(cont, e.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } 

} 

`

+0

知道了,那麼,我需要一個幫手權管理的所有表 –

相關問題