将SQLiteHelper与另一个源文件连接起来

时间:2012-12-11 14:42:34

标签: android sqlite

我有一个SQLiteHelper源文件,我用它来创建数据库,数据等等。我有另一个源文件需要获取SQLiteHelper源文件创建的数据。我怎么能做到这一点?更具体地说,我如何连接两个源文件?如果没有我将其指向由SQLiteHelper创建的数据库(如何将其指向数据库?),则无法使用GetReadableDatabase()。谢谢。

1 个答案:

答案 0 :(得分:0)

在SQLiteOpenHelper类中实现标准CRUD操作,然后在您的活动中实例化您的SQLite Helper(使用单个静态实例可能更好)。通过该实例访问您的数据库。

这是一个Helper类的片段,我曾经使用过一段时间用于Facebook CHAT应用程序。

/**
 * @author vinaysshenoy
 * Manages all database aspects
 */
public class MyDatabaseHelper extends SQLiteOpenHelper {

        //Database properties
        public static final String DATABASE_NAME = "database.db";
        public static final int DATABASE_VERSION = 1;

        //Database Table Names
        public static final String TABLE_FRIENDS = "Friends";
        public static final String TABLE_CONVERSATIONS = "Conversations";

        //Friends Table Columns
        public static final String FRIENDS_COLUMN_FB_USER_ID = "fb_user_id";
        public static final String FRIENDS_COLUMN_FB_USERNAME = "fb_username";
        public static final String FRIENDS_COLUMN_FB_FULL_NAME = "fb_full_name";

        //Conversations Table Columns
        public static final String CONVERSATIONS_COLUMN_ID = "_id";
        public static final String CONVERSATIONS_COLUMN_USER_ID = "user_id";
        public static final String CONVERSATIONS_COLUMN_FRIEND_ID = "friend_id";
        public static final String CONVERSATIONS_COLUMN_ENDED = "ended";


        //Database Creation SQL
        static final String CREATE_TABLE_FRIENDS = "CREATE TABLE "
                        + TABLE_FRIENDS + "("
                        + FRIENDS_COLUMN_FB_USER_ID + " integer primary key,"
                        + FRIENDS_COLUMN_FB_USERNAME + " text not null unique,"
                        + FRIENDS_COLUMN_FB_FULL_NAME + " text not null"
                        + ");";

        public static final String CREATE_TABLE_CONVERSATIONS = "CREATE TABLE "
                        + TABLE_CONVERSATIONS + "("
                        + CONVERSATIONS_COLUMN_ID + " integer primary key autoincrement,"
                        + CONVERSATIONS_COLUMN_USER_ID + " integer not null,"
                        + CONVERSATIONS_COLUMN_FRIEND_ID + " integer not null,"
                        + CONVERSATIONS_COLUMN_ENDED + " integer not null"
                        + ");";

        public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version) {
                super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
                createFriendsTable(db);
                createConversationsTable(db);
        }

       private void createConversationsTable(SQLiteDatabase db) {
                db.execSQL(CREATE_TABLE_CONVERSATIONS);
        }

        private void createFriendsTable(SQLiteDatabase db) {
                db.execSQL(CREATE_TABLE_FRIENDS);
        }

         /**
         * @param table Name of the table to query
         * @param columns Array of column names to fetch
         * @param selection WHERE clause
         * @param selectionArgs WHERE clause arguments
         * @param groupBy GROUP BY clause
         * @param having HAVING clause
         * @param orderBy ORDER BY clause
         * @return Cursor to the requested rows
         */
        public Cursor getCursor(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {

                return getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
        }

        /**
         * Insert or replace a single row into a table
         * @param table Name of the table
         * @param nullColumnHack null hack
         * @param values Values to insert
         */
        public void putData(String table, String nullColumnHack, ContentValues values) {

                SQLiteDatabase db = getWritableDatabase();
                db.replace(table, nullColumnHack, values);
                db.close();
        }

        /**
         * Insert or replace an array of rows into a table
         * @param table Name of the table
         * @param nullColumnHack null hack
         * @param values Array of values to insert
         */
        public void putData(String table, String nullColumnHack, ContentValues[] values) {

                SQLiteDatabase db = getWritableDatabase();
                try {
                        db.beginTransaction();
                        for(ContentValues value : values) {
                                db.replace(table, nullColumnHack, value);
                        }
                        db.setTransactionSuccessful();
                }
                catch(Exception e) {
                        e.printStackTrace();
                }
                finally {
                        db.endTransaction();
                }

                db.close();
        }

        /**
         * Delete rows from a table
         * @param table Name of the table
         * @param whereClause WHERE clause
         * @param whereArgs WHERE clause arguments
         */
        public void deleteData(String table, String whereClause, String[] whereArgs) {

                SQLiteDatabase db = getWritableDatabase();
                db.delete(table, whereClause, whereArgs);
                db.close();
        }

        /**
         * Update a table
         * @param table Name of the table
         * @param values Values to update
         * @param whereClause WHERE clause
         * @param whereArgs WHERE clause arguments
         */
        public void updateData(String table, ContentValues values, String whereClause, String[] whereArgs) {

                SQLiteDatabase db = getWritableDatabase();
                db.update(table, values, whereClause, whereArgs);
                db.close();
        }

}

然后在您的活动中,执行MyDatabaseHelper dbHelper = new MyDatabaseHelper(),然后定义调用dbHelper.getCuror()或其他方法时所需的投影和选择。

这段代码是在我刚刚开始编程的时候编写的,所以从结构POV来看,它编写起来非常糟糕(至少,我现在对此感觉如何),但这就是它的工作方式。

编辑:使用

初始化数据库助手
databaseHelper = new MyDatabaseHelper(
                                        HomeActivity.this,
                                        MyDatabaseHelper.DATABASE_NAME, null,
                                        MyDatabaseHelper.DATABASE_VERSION);