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

歡迎訪問 生活随笔!

生活随笔

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

Android

Android SQLite详解

發(fā)布時間:2023/11/29 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android SQLite详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在項目開發(fā)中,我們或多或少都會用到數(shù)據(jù)庫。在Android中,我們一般使用SQLite,因為Android在android.database.sqlite包封裝了很多SQLite操作的API。我自己寫了一個Demo來總結(jié)SQLite的使用,托管在Github上,大家可以點擊下載APK,也可以點擊下載源碼。Demo截圖如下:

在使用SQLite時,我建議先下載一個本地SQLite客戶端來驗證操作,在本地寫的SQL語句運行正確后,再轉(zhuǎn)移到Android中。我用的是SQLite Expert Personal。
首先創(chuàng)建一個繼承在SQLiteOpenHelper的類,并重寫onCreate()和onUpgrade()方法。

public class OrderDBHelper extends SQLiteOpenHelper{ private static final int DB_VERSION = 1; private static final String DB_NAME = "myTest.db"; public static final String TABLE_NAME = "Orders"; public OrderDBHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { // create table Orders(Id integer primary key, CustomName text, OrderPrice integer, Country text); String sql = "create table if not exists " + TABLE_NAME + " (Id integer primary key, CustomName text, OrderPrice integer, Country text)"; sqLiteDatabase.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; sqLiteDatabase.execSQL(sql); onCreate(sqLiteDatabase); } }

這個類主要用于建數(shù)據(jù)庫和建表用,我們再創(chuàng)建一個OrderDao用于處理所有的數(shù)據(jù)操作方法。在OrderDao鐘實例化OrderDBHelper:

public OrderDao(Context context) {this.context = context;ordersDBHelper = new OrderDBHelper(context); }

數(shù)據(jù)庫操作無外乎:“增刪查改”。對于“增刪改”這類對表內(nèi)容變換的操作,我們需先調(diào)用getWritableDatabase(),在執(zhí)行的時候可以調(diào)用通用的execSQL(String sql)方法或?qū)?yīng)的操作API:insert()、delete()、update()。而對“查”,需要調(diào)用getReadableDatabase(),這時就不能使用execSQL方法了,得使用query()或rawQuery()方法。下面開始一一介紹。

增加數(shù)據(jù)

在我的Demo中,有兩種增加數(shù)據(jù)操作:
初始化數(shù)據(jù)
在進入Demo程序時,先判斷表中是否有數(shù)據(jù),如果表中沒有數(shù)據(jù),我將先添加一些數(shù)據(jù)。在初始化數(shù)據(jù)時,因為一次性要添加的數(shù)據(jù)比較多,所以我直接采用的是execSQL方法:

db = ordersDBHelper.getWritableDatabase(); db.beginTransaction();db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (1, 'Arc', 100, 'China')"); db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (2, 'Bor', 200, 'USA')"); db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (3, 'Cut', 500, 'Japan')"); db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (4, 'Bor', 300, 'USA')"); db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (5, 'Arc', 600, 'China')"); db.execSQL("insert into " + OrderDBHelper.TABLE_NAME + " (Id, CustomName, OrderPrice, Country) values (6, 'Doom', 200, 'China')"); db.setTransactionSuccessful();

插入一條新數(shù)據(jù)
我們還可以使用insert(String table,String nullColumnHack,ContentValues values)方法來插入,ContentValues內(nèi)部實現(xiàn)就是HashMap,但是兩者還是有差別的,ContenValuesKey只能是String類型,Value只能存儲基本類型的數(shù)據(jù),像string,int之類的,不能存儲對象這種東西:

public ContentValues() {// Choosing a default size of 8 based on analysis of typical// consumption by applications.mValues = new HashMap<String, Object>(8); }

使用insert()方法我們插入一條新數(shù)據(jù)(7, "Jne", 700, "China"),對于修改數(shù)據(jù)的操作我們一般當作事務(wù)(Transaction)處理:

db = ordersDBHelper.getWritableDatabase(); db.beginTransaction();// insert into Orders(Id, CustomName, OrderPrice, Country) values (7, "Jne", 700, "China"); ContentValues contentValues = new ContentValues(); contentValues.put("Id", 7); contentValues.put("CustomName", "Jne"); contentValues.put("OrderPrice", 700); contentValues.put("Country", "China"); db.insertOrThrow(OrderDBHelper.TABLE_NAME, null, contentValues); db.setTransactionSuccessful();

刪除數(shù)據(jù)

刪除數(shù)據(jù)的方法除了execSQL還有delete(String table,String whereClause,String[] whereArgs),whereClause是刪除條件,whereArgs是刪除條件值數(shù)組。

db = ordersDBHelper.getWritableDatabase(); db.beginTransaction();// delete from Orders where Id = 7 db.delete(OrderDBHelper.TABLE_NAME, "Id = ?", new String[]{String.valueOf(7)}); db.setTransactionSuccessful();

再看刪除的源碼,里面會拼裝刪除條件和刪除條件值數(shù)組:

public int delete(String table, String whereClause, String[] whereArgs) { acquireReference(); try { SQLiteStatement statement = new SQLiteStatement(this, "DELETE FROM " + table + (!TextUtils.isEmpty(whereClause) ? " WHERE " + whereClause : ""), whereArgs); try { return statement.executeUpdateDelete(); } finally { statement.close(); } } finally { releaseReference(); } }

修改數(shù)據(jù)

修改數(shù)據(jù)和插入數(shù)據(jù)很相似,調(diào)用的方法除了execSQL還可以是update(String table,ContentValues values,String whereClause, String[] whereArgs):

db = ordersDBHelper.getWritableDatabase(); db.beginTransaction();// update Orders set OrderPrice = 800 where Id = 6 ContentValues cv = new ContentValues(); cv.put("OrderPrice", 800); db.update(OrderDBHelper.TABLE_NAME, cv, "Id = ?", new String[]{String.valueOf(6)}); db.setTransactionSuccessful();

查找數(shù)據(jù)

查找數(shù)據(jù)有兩個方法,一是public Cursor query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String orderBy,String limit);,另外一個是public Cursor rawQuery(String sql, String[] selectionArgs)。rawQuery的寫法類似上面的execSQL,在此不做介紹,query方法中的參數(shù)如下:

  • table:表名稱
  • columns:列名稱數(shù)組
  • selection:條件字句,相當于where
  • selectionArgs:條件字句,參數(shù)數(shù)組
  • groupBy:分組列
  • having:分組條件
  • orderBy:排序列
  • limit:分頁查詢限制
  • Cursor:返回值,相當于結(jié)果集ResultSet

我們可以看到返回的類型都是Cursor,Cursor是一個游標接口,提供了遍歷查詢結(jié)果的方法,如移動指針方法move(),獲得列值方法。Cursor游標常用方法如下:

我們先來查一查用戶名為"Bor"的信息:

db = ordersDBHelper.getReadableDatabase();// select * from Orders where CustomName = 'Bor' cursor = db.query(OrderDBHelper.TABLE_NAME,ORDER_COLUMNS,"CustomName = ?",new String[] {"Bor"}, null, null, null); if (cursor.getCount() > 0) { List<Order> orderList = new ArrayList<Order>(cursor.getCount()); while (cursor.moveToNext()) { Order order = parseOrder(cursor); orderList.add(order); } return orderList; }

當然我們也可以查詢總數(shù)、最大值最小值之類的,以查詢Country為China的用戶總數(shù)為例:

db = ordersDBHelper.getReadableDatabase(); // select count(Id) from Orders where Country = 'China' cursor = db.query(OrderDBHelper.TABLE_NAME,new String[]{"COUNT(Id)"}, "Country = ?", new String[] {"China"}, null, null, null); if (cursor.moveToFirst()) { count = cursor.getInt(0); }

至此SQLite就介紹完了,大家可以下載Demo詳細查看。Demo中還有一些其他比較干貨的東西,大家可以挖掘挖掘。當然Demo中也有些不足,我還會更新,盡量做到讓用戶通過Demo就能學會如何使用SQLite。

轉(zhuǎn)載地址:http://www.jianshu.com/p/5c33be6ce89d

轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/8778677.html

總結(jié)

以上是生活随笔為你收集整理的Android SQLite详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。