Android DB助手类

时间:2011-12-31 19:15:22

标签: android class helper

我正在编写一个通用的SQLHelper类,用于我的android项目。但是我对android很新手,因此我不知道应该使用哪些库来返回数据。我的DBHelper扩展了SQLiteOpenHelper,我使用了该类的方法。

对于“选择”查询,我应该返回哪种类型? (如.NET中的DataSet) 对于“更新”查询,我应该获得什么作为参数?

我想生成SQL查询。

即。对于插入,我使用了ContentValuesTableName参数。

1 个答案:

答案 0 :(得分:3)

看看我用来制作示例项目的代码。它为您提供了执行和使用SQLitedatabase所需的一切。它回答了你的两个问题。 该示例获取用户的书名,作者,isbn编号,评级和书上的状态,将其插入数据库,允许将数据更新删除并逐个获取。

    public class DbAdapter {


    private static final String DATABASE_NAME = " nba";
    private static final String DATABASE_TABLE = "bookList";
    private static final int DATABASE_VERSION = 1;
    private static String TAG = "Upgrading Database!";
    public static final String KEY_BOOK = "book";
    public static final String KEY_AUTHOR = "author";
    public static final String KEY_ISBN = "isbn";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_RATING = "rating";
    public static final String KEY_STATUS = "status";


    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;


    private static final String DATABASE_CREATE = 
            " create table  " +  DATABASE_TABLE  + " ("
            + KEY_ROWID + " integer primary key autoincrement,  "
            + KEY_AUTHOR + " text not null, "
            + KEY_BOOK + " text not null, "
            + KEY_RATING + " text not null, "
            + KEY_STATUS + " text not null, "
            + KEY_ISBN + " text not null); ";

    private final Context mCtx;


    public DbAdapter (Context ctx){
        this.mCtx = ctx;



            }

            private static class DatabaseHelper extends SQLiteOpenHelper{
            DatabaseHelper(Context context){
                super(context, DATABASE_NAME, null, DATABASE_VERSION);


            }

            @Override
            public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);






            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                  db.execSQL("ALTER TABLE bookList ADD COLUMN String status");

        }
    }
            public DbAdapter open() throws SQLException{

                mDbHelper =  new DatabaseHelper(mCtx);
                mDb = mDbHelper.getWritableDatabase();
                return this;
            }
            public void close(){
                mDbHelper.close();
            }

            public long createBook(String book, String author, String isbn, float rating, String status){
                ContentValues initialValues = new ContentValues();
                initialValues.put(KEY_BOOK, book);
                initialValues.put(KEY_AUTHOR, author);
                initialValues.put(KEY_ISBN, isbn);
                initialValues.put(KEY_RATING, rating);
                initialValues.put(KEY_STATUS, status);



                return mDb.insert(DATABASE_TABLE, null, initialValues);

            }
            public boolean deleteBook(long rowId){
                return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;

            }
            public Cursor fetchAllBooks(){          
                return mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_ISBN,KEY_AUTHOR,KEY_ROWID, KEY_RATING, KEY_STATUS}, null, null, null, null, null);
            }
            public Cursor fetchBook(long rowId) throws SQLException{
                Cursor mCursor = 
                mDb.query(DATABASE_TABLE, new String[]{KEY_BOOK, KEY_AUTHOR,KEY_ROWID, KEY_ISBN, KEY_RATING, KEY_STATUS}, KEY_ROWID + "=" +
                            rowId, null, null, null, null);

                if(mCursor != null){
                    mCursor.moveToFirst();
                }
                return mCursor;

            }
            public boolean updateBook(long rowId, String book, String author, String isbn, float rating, String status){
                ContentValues args = new ContentValues();
                args.put(KEY_BOOK, book);
                args.put(KEY_AUTHOR, author);
                args.put(KEY_ISBN, isbn);
                args.put(KEY_RATING, rating);
                args.put(KEY_STATUS, status);


                return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null)> 0;


            }


}