您好,登錄后才能下訂單哦!
這篇“Android在Sqlite3中怎么應(yīng)用”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android在Sqlite3中怎么應(yīng)用”文章吧。
1、首先先建立一個(gè)DatabaseHelper類,繼承自SQLiteOpenHelper,用于建立數(shù)據(jù)庫,并可更新數(shù)據(jù)庫例如我新建了兩張表
public class DatabaseHelper extends SQLiteOpenHelper { private static DatabaseHelper _databaseHelper; private static final String TAG = DatabaseHelper.class.getSimpleName(); public synchronized static DatabaseHelper getInstance(Context context) { if(_databaseHelper==null) { _databaseHelper = new DatabaseHelper(context,"cgjlb",null,2); } return _databaseHelper; } public synchronized static void destoryInstance(){ if(_databaseHelper!=null){ _databaseHelper=null; public DatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { //主表記錄 String sql = "create table t_share(_id integer primary key autoincrement,sharenum text,sharename text,shareprice real,sharelastprice real,addtime text,uptime text,bl real)"; sqLiteDatabase.execSQL(sql); //記錄歷史表 sql = "create table t_sharehis(_id integer primary key autoincrement,shareid integer, shareprice real,uptime text)"; public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { Log.d(TAG, "onUpgrade: upddatabase"); sqLiteDatabase.execSQL("drop table t_share"); sqLiteDatabase.execSQL("drop table t_sharehis"); onCreate(sqLiteDatabase); }
2、編寫數(shù)據(jù)庫具體操作類、增刪改查我都寫了
public class ShareDbService { SQLiteDatabase db = null; DatabaseHelper databaseHelper; private static final String TAG = ShareDbService.class.getSimpleName(); public ShareDbService(Context context) { databaseHelper = DatabaseHelper.getInstance(context); db = databaseHelper.getWritableDatabase(); } public void addShareInfo(ShareBean shareBean) { int Id = getShareId(shareBean.getShareNum()); if(Id==-1) { ContentValues values = new ContentValues(); values.put("sharenum", shareBean.getShareNum()); values.put("sharename", shareBean.getShareName()); values.put("shareprice", shareBean.getSharePrice()); values.put("sharelastprice", shareBean.getShareLastPrice()); values.put("uptime", shareBean.getUptime()); values.put("addtime",shareBean.getAddtime()); double shareprice = shareBean.getSharePrice(); double sharelastprice = shareBean.getShareLastPrice(); double bl = (sharelastprice-shareprice)/shareprice*100; values.put("bl", new DecimalFormat("0.00").format(bl)); db = databaseHelper.getWritableDatabase(); db.insert("t_share", null, values); ShareHisBean shareHisBean = new ShareHisBean(); shareHisBean.setShareId(getShareId(shareBean.getShareNum())); shareHisBean.setSharePrice(shareBean.getSharePrice()); shareHisBean.setUpTime(shareBean.getUptime()); addShareHis(shareHisBean); db.close(); } private void addShareHis(ShareHisBean shareHisBean){ ContentValues values = new ContentValues(); values.put("shareid", shareHisBean.getShareId()); values.put("shareprice", shareHisBean.getSharePrice()); values.put("uptime", shareHisBean.getUptime()); db.insert("t_sharehis", null, values); db.close(); public void refreshShare(ShareHisBean shareHisBean,double sharePrice){ try { //更新信息主表 values.put("uptime", shareHisBean.getUptime()); values.put("sharelastprice", shareHisBean.getSharePrice()); String[] args = {String.valueOf(shareHisBean.getShareId())}; double sharelastprice = shareHisBean.getSharePrice(); double bl = (sharelastprice - sharePrice) / sharePrice * 100; values.put("bl", bl); db.update("t_share", values, "_Id=?", new String[]{String.valueOf(shareHisBean.getShareId())}); //添加記錄 values = new ContentValues(); values.put("shareid", shareHisBean.getShareId()); values.put("shareprice", shareHisBean.getSharePrice()); db.insert("t_sharehis", null, values); }catch(Exception ex){ ex.printStackTrace(); finally { public void delShare(String Id){ db.delete("t_share","_Id=?",new String[]{Id}); db.delete("t_sharehis","shareid=?",new String[]{Id}); private int getShareId(String shareNum) { db = databaseHelper.getReadableDatabase(); String sql = "select _Id from t_share where sharenum='" + shareNum + "'"; int Id = -1; Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { while (cursor.moveToNext()){ Id = cursor.getInt(0); } cursor.close(); return Id; public List<ShareBean> getShareList(){ List<ShareBean> shareBeans = new ArrayList<>(); String sql = "select _id,sharenum,sharename,shareprice,sharelastprice,uptime,addtime,bl from t_share order by _Id desc"; ShareBean shareBean = new ShareBean(); shareBean.set_Id(cursor.getInt(0)); shareBean.setShareNum(cursor.getString(1)); shareBean.setShareName(cursor.getString(2)); shareBean.setSharePrice(cursor.getDouble(3)); shareBean.setShareLastPrice(cursor.getDouble(4)); shareBean.setUptime(cursor.getString(5)); shareBean.setAddtime(cursor.getString(6)); shareBean.setBl(cursor.getDouble(7)); shareBeans.add(shareBean); return shareBeans; public List<ShareHisBean> getShareListHis(int ShareId){ List<ShareHisBean> shareHisBeans = new ArrayList<>(); String sql = "select _id,shareid,shareprice,uptime from t_sharehis where shareid='"+String.valueOf(ShareId)+"' order by _id desc"; ShareHisBean shareHisBean = new ShareHisBean(); shareHisBean.set_Id(cursor.getInt(0)); shareHisBean.setShareId(cursor.getInt(1)); shareHisBean.setSharePrice(cursor.getDouble(2)); shareHisBean.setUpTime(cursor.getString(3)); shareHisBeans.add(shareHisBean); return shareHisBeans; public ShareBean getShareInfo(int ShareId) { ShareBean shareBean = new ShareBean(); //_id integer primary key autoincrement,sharenum text,sharename text,shareprice real,sharelastprice real,addtime text,uptime text,bl real String sql = "select _id,sharenum,sharename,shareprice,sharelastprice,addtime,uptime,bl from t_share where _Id='"+String.valueOf(ShareId)+"'"; if (cursor.moveToNext()){ shareBean.setAddtime(cursor.getString(5)); shareBean.setUptime(cursor.getString(6)); return shareBean; }
3、實(shí)際中的應(yīng)用,就舉一個(gè)例子吧,數(shù)據(jù)庫操作不用開新線程,直接在主線程里弄就行
ShareDbService shareDbService = new ShareDbService(getContext()); shareDbService.delShare(String.valueOf(shareBean.get_Id()));
4、需要注意的是SQLite不支持多線程,最好是能批量修改,否則很麻煩,如果一定要多線程 ,比如循環(huán)請(qǐng)求網(wǎng)絡(luò)更新數(shù)據(jù)庫時(shí),可以使用CountDownLatch,先將數(shù)據(jù)弄在一個(gè)集合里,等子線程全部執(zhí)行完成后,再更新數(shù)據(jù)庫。此方法具體應(yīng)用如下
先聲明一個(gè)變量
CountDownLatch latch;
實(shí)際應(yīng)用代碼
//設(shè)置多線程 個(gè)數(shù),一共用多少個(gè)線程一定要寫清楚,后面是要減的。 latch = new CountDownLatch(count); for (int i = 0; i < count; i++) { new Thread(()-> { try { //網(wǎng)絡(luò)請(qǐng)求等操作 } catch (Exception e) { e.printStackTrace(); } finally { //這里會(huì)自動(dòng)減1操作,一定要寫。 latch.countDown(); } }).start(); }; try { latch.await(); //最后執(zhí)行主程序,比如更新數(shù)據(jù)庫 Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show(); } catch (InterruptedException e) { e.printStackTrace(); }
以上就是關(guān)于“Android在Sqlite3中怎么應(yīng)用”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。