简体   繁体   English

如何在Android应用程序中构造数据库访问方法?

[英]How should I structure database access methods in my Android app?

I am developing an Android application that has a database having 14-15 tables. 我正在开发一个Android应用程序,该应用程序的数据库包含14-15个表。 I want to access these tables obviously for querying, inserting, deleting etc. 我想访问这些表显然是为了查询,插入,删除等。

Should I make a single helper that would open the database and then create a single adapter that would access it and query it as I plan to? 我是否应该创建一个单个的助手来打开数据库,然后创建一个单个适配器来按计划访问并查询它? Or make a helper for each table? 还是为每个桌子做一个助手?

I am confused about this point. 我对此感到困惑。 The first option seems more realistic to me because I wouldn't need to open the database multiple times. 第一种选择对我来说似乎更现实,因为我不需要多次打开数据库。

Create a class that extends from SQLiteOpenHelper and make method for each one. 创建一个从SQLiteOpenHelper扩展的类,并为每个类创建方法。 One for insert, one for delete, one for update and one from retrieving ResultSet. 一种用于插入,一种用于删除,一种用于更新,另一种用于检索ResultSet。 Here is an example: 这是一个例子:

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();
        }
    }

}

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何构建我的应用程序? - How should i structure my app? 我应该如何处理我的应用程序的结构(ListView初始)? - How should I handle the structure of my app (ListView inception)? 如何使用WebView在android应用程序中访问我的Web应用程序JavaScript方法? - How can I access my web app JavaScript methods in android application using WebView? 如何将我的应用程序数据保存在我的android应用程序中? - How should i save my app data in my android app? 领域数据库-如何构建我的Android应用程序 - Realm database - how do I structure my Android application 如何从JUnit测试获取对我的Android应用程序的数据库和上下文的访问权限? - How can I acquire access to my android app's database and context from JUnit test? 如何防止黑客在 android 上访问我的应用程序的数据库? - How to prevent hacker access my app's database on android? 我应如何为我的Android应用存储数据? - How Should I Store Data for My Android App? 如何发布多种版本的Android应用程序? - How should I release multiple flavours of my Android app? Android:如果我需要从远程数据库获取项目和标题,我应该如何构建我的Activity? - Android: How should I structure my Activity if I need to get an item and title from a remote db?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM