In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to apply Android in Sqlite3", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to apply Android in Sqlite3" article.
1. First, set up a DatabaseHelper class, which inherits from SQLiteOpenHelper, and can be used to build and update the database. For example, I have created two new tables.
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 (_ database Helperware 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) {/ / Master table record 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) / / record history table 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. I have written the specific operation classes, additions, deletions, changes and queries of the database.
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 {/ / update the main table 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 ())}); / / add records 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 getShareList () {List 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 getShareListHis (int ShareId) {List 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. For practical applications, let me cite an example. Database operations do not need to open new threads, but can be done directly in the main thread.
ShareDbService shareDbService = new ShareDbService (getContext ()); shareDbService.delShare (String.valueOf (shareBean.get_Id ()
4. It should be noted that SQLite does not support multithreading, and it is best to modify it in batches, otherwise it is very troublesome. If you must use multithreading, such as looping requests for network updates to the database, you can use CountDownLatch to put the data in a collection first, and then update the database after all the execution of the child threads is completed. The specific application of this method is as follows
Declare a variable first
CountDownLatch latch
Practical application code
/ / set the number of multithreads, and the total number of threads used must be clearly written, and then be reduced. Latch = new CountDownLatch (count); for (int I = 0; I
< count; i++) { new Thread(()->{try {/ / Network request and other operations} catch (Exception e) {e.printStackTrace ();} finally {/ / the operation is automatically minus 1 here, be sure to write. Latch.countDown ();}}) .start ();}; try {latch.await (); / / finally execute the main program, such as updating the database Toast.makeText (getContext (), "refresh successfully", Toast.LENGTH_SHORT). Show ();} catch (InterruptedException e) {e.printStackTrace () } the above is about the content of this article on "how to use Android in Sqlite3". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.