我应该如何在我的Android应用程序中构建数据库访问方法?

时间:2013-01-16 01:38:57

标签: android database sqlite sqliteopenhelper

我正在开发一个Android应用程序,它有一个包含14-15个表的数据库。我想显然访问这些表以查询,插入,删除等。

我是否应该创建一个可以打开数据库的单个帮助程序,然后创建一个可以访问它并按照我的计划查询它的单个适配器?或者为每张桌子做一个帮手?

我对这一点很困惑。第一个选项对我来说似乎更现实,因为我不需要多次打开数据库。

1 个答案:

答案 0 :(得分: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();
        }
    }

}

`