0

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

4

1 回答 1

0

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

这是我不久前在 Facebook 聊天应用程序中使用的 Helper 类的片段。

/**
 * @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);
于 2012-12-11T14:55:39.653 回答