日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android sqlite 操作类封装,[Android] Sqlite 数据库操做 工具封装类

發布時間:2023/12/10 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android sqlite 操作类封装,[Android] Sqlite 数据库操做 工具封装类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

sqlite 數據庫封裝類html

DatabaseUtil.java(封裝的類)java

packagecom.jack.androidbase.tools;importandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.SQLException;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.util.Log;/*** sqlite 數據庫操做類*/

public classDatabaseUtil {private static final String TAG = "DatabaseUtil";/*** Database Name*/

private static final String DATABASE_NAME = "student_data";/*** Database Version*/

private static final int DATABASE_VERSION = 1;/*** Table Name*/

private static final String DATABASE_TABLE = "tb_student";/*** Table columns*/

public static final String KEY_NAME = "name";public static final String KEY_GRADE = "grade";public static final String KEY_ROWID = "_id";/*** Database creation sql statement*/

private static final String CREATE_TABLE =

"create table " + DATABASE_TABLE + " (" + KEY_ROWID + " integer primary key autoincrement, "

+ KEY_NAME + " text not null, " + KEY_GRADE + " text not null);";/*** Context*/

private finalContext mCtx;privateDatabaseHelper mDbHelper;privateSQLiteDatabase mDb;/*** Inner private class. Database Helper class for creating and updating database.*/

private static class DatabaseHelper extendsSQLiteOpenHelper {

DatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);

}/*** onCreate method is called for the 1st time when database doesn't exists.*/@Overridepublic voidonCreate(SQLiteDatabase db) {

Log.i(TAG,"Creating DataBase: " +CREATE_TABLE);

db.execSQL(CREATE_TABLE);

}/*** onUpgrade method is called when database version changes.*/@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, intnewVersion) {

Log.w(TAG,"Upgrading database from version " + oldVersion + " to "

+newVersion);

}

}/*** Constructor - takes the context to allow the database to be

* opened/created

*

*@paramctx the Context within which to work*/

publicDatabaseUtil(Context ctx) {this.mCtx =ctx;

}/*** This method is used for creating/opening connection

*

*@returninstance of DatabaseUtil

*@throwsSQLException*/

public DatabaseUtil open() throwsSQLException {

mDbHelper= newDatabaseHelper(mCtx);

mDb=mDbHelper.getWritableDatabase();return this;

}/*** This method is used for closing the connection.*/

public voidclose() {

mDbHelper.close();

}/*** This method is used to create/insert new record record.

*

*@paramname

*@paramgrade

*@returnlong*/

public longinsert(String name, String grade) {

ContentValues initialValues= newContentValues();

initialValues.put(KEY_NAME, name);

initialValues.put(KEY_GRADE, grade);return mDb.insert(DATABASE_TABLE, null, initialValues);

}/*** This method will delete record.

*

*@paramrowId

*@returnboolean*/

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

}/*** This method will deleteAll record.

*

*@return

*/

public booleandeleteAll() {return mDb.delete(DATABASE_TABLE, " 1 ", null) > 0;

}/*** This method will return Cursor holding all the records.

*

*@returnCursor*/

publicCursor fetchAll() {return mDb.query(DATABASE_TABLE, newString[]{KEY_ROWID, KEY_NAME,

KEY_GRADE},null, null, null, null, null);

}/*** This method will return Cursor holding the specific record.

*

*@paramid

*@returnCursor

*@throwsSQLException*/

public Cursor fetch(long id) throwsSQLException {

Cursor mCursor=mDb.query(true, DATABASE_TABLE, newString[]{KEY_ROWID,

KEY_NAME, KEY_GRADE}, KEY_ROWID+ "=" + id, null,null, null, null, null);if (mCursor != null) {

mCursor.moveToFirst();

}returnmCursor;

}/*** This method will update record.

*

*@paramid

*@paramname

*@paramstandard

*@returnboolean*/

public boolean update(intid, String name, String standard) {

ContentValues args= newContentValues();

args.put(KEY_NAME, name);

args.put(KEY_GRADE, standard);return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + id, null) > 0;

}

}

使用詳解:android

DatabaseUtil dbUtil = new DatabaseUtil(this);

dbUtil.open();

switch (v.getId()) {

case R.id.other_btn_sqlite_list: //查詢

Cursor cursor = dbUtil.fetchAll();

if (cursor != null) {

while (cursor.moveToNext()) {

Log.i("Student", "ID:" + cursor.getInt(0) + ",Student Name: " + cursor.getString(1) +

",Grade: " + cursor.getString(2));

}

}

break;

case R.id.other_btn_sqlite_add:

dbUtil.insert("Prashant Thakkar", "100");

Log.i("Student", "add over");

break;

case R.id.other_btn_sqlite_update:

dbUtil.update(1, "aa", "bb");

Log.i("Student", "update over");

break;

case R.id.other_btn_sqlite_del:

dbUtil.delete(2);

Log.i("Student", "delete over");

break;

case R.id.other_btn_sqlite_del_all:

dbUtil.deleteAll();

Log.i("Student", "delete all over");

break;

}

dbUtil.close();sql

參考網址:數據庫

https://www.cnblogs.com/sowhat4999/p/4439856.htmlide

https://www.cnblogs.com/ProMonkey/p/5765616.html?? (有添加單例模式)fetch

本博客地址: wukong1688this

轉載請著名出處!謝謝~~spa

總結

以上是生活随笔為你收集整理的android sqlite 操作类封装,[Android] Sqlite 数据库操做 工具封装类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。