Android在Sqlite3中的应用及多线程使用数据库的建议
2022/4/24 19:13:16
本文主要是介绍Android在Sqlite3中的应用及多线程使用数据库的建议,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1、首先先建立一个DatabaseHelper类,继承自SQLiteOpenHelper,用于建立数据库,并可更新数据库例如我新建了两张表
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)"; sqLiteDatabase.execSQL(sql); } @Override 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、编写数据库具体操作类、增删改查我都写了
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 = databaseHelper.getWritableDatabase(); db.insert("t_sharehis", null, values); db.close(); } public void refreshShare(ShareHisBean shareHisBean,double sharePrice){ try { //更新信息主表 db = databaseHelper.getWritableDatabase(); ContentValues values = new ContentValues(); 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()); values.put("uptime", shareHisBean.getUptime()); db.insert("t_sharehis", null, values); }catch(Exception ex){ ex.printStackTrace(); } finally { db.close(); } } public void delShare(String Id){ db = databaseHelper.getWritableDatabase(); db.delete("t_share","_Id=?",new String[]{Id}); db.delete("t_sharehis","shareid=?",new String[]{Id}); db.close(); } 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(); db.close(); return Id; } public List<ShareBean> getShareList(){ db = databaseHelper.getReadableDatabase(); List<ShareBean> shareBeans = new ArrayList<>(); String sql = "select _id,sharenum,sharename,shareprice,sharelastprice,uptime,addtime,bl from t_share order by _Id desc"; Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { while (cursor.moveToNext()){ 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); } } cursor.close(); db.close(); return shareBeans; } public List<ShareHisBean> getShareListHis(int ShareId){ db = databaseHelper.getReadableDatabase(); List<ShareHisBean> shareHisBeans = new ArrayList<>(); String sql = "select _id,shareid,shareprice,uptime from t_sharehis where shareid='"+String.valueOf(ShareId)+"' order by _id desc"; Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { while (cursor.moveToNext()){ 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); } } cursor.close(); db.close(); return shareHisBeans; } public ShareBean getShareInfo(int ShareId) { db = databaseHelper.getReadableDatabase(); 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)+"'"; Cursor cursor = db.rawQuery(sql, null); if (cursor != null) { if (cursor.moveToNext()){ 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.setAddtime(cursor.getString(5)); shareBean.setUptime(cursor.getString(6)); shareBean.setBl(cursor.getDouble(7)); } } cursor.close(); db.close(); return shareBean; } }
3、实际中的应用,就举一个例子吧,数据库操作不用开新线程,直接在主线程里弄就行
ShareDbService shareDbService = new ShareDbService(getContext()); shareDbService.delShare(String.valueOf(shareBean.get_Id()));
4、需要注意的是SQLite不支持多线程,最好是能批量修改,否则很麻烦,如果一定要多线程 ,比如循环请求网络更新数据库时,可以使用CountDownLatch,先将数据弄在一个集合里,等子线程全部执行完成后,再更新数据库。此方法具体应用如下
先声明一个变量
CountDownLatch latch;
实际应用代码
//设置多线程 个数,一共用多少个线程一定要写清楚,后面是要减的。 latch = new CountDownLatch(count); for (int i = 0; i < count; i++) { new Thread(()-> { try { //网络请求等操作 } catch (Exception e) { e.printStackTrace(); } finally { //这里会自动减1操作,一定要写。 latch.countDown(); } }).start(); }; try { latch.await(); //最后执行主程序,比如更新数据库 Toast.makeText(getContext(), "刷新成功", Toast.LENGTH_SHORT).show(); } catch (InterruptedException e) { e.printStackTrace(); }
这篇关于Android在Sqlite3中的应用及多线程使用数据库的建议的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路