In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to package sqlite to APK for release in android. Many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.
The normal application database is placed in / data/data/ package name / database/test.db. When the application is published, the database will not be released with the application.
So in order for the data we have prepared to use properly, we must be able to copy the database itself to the SD card.
Copy the resources under res/raw/test.db to the / mnt/sdcard/test/test.db under SD card
Package your.wzf.namespace.database;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import your.wzf.namespace.R;import android.content.Context;public class CopyDatabase {private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory (). GetAbsolutePath () + "/ nqxx_512022"; private final String DATABASE_FILENAME = "zylz.db"; private Context context Public CopyDatabase (Context context) {this.context = context;} public void OpenDataBase () {try {String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; File dir = new File (DATABASE_PATH) / / determine whether there is a directory for storing the database under the SD card. If not, create a new directory if (! dir.exists ()) {dir.mkdir () } try {/ / if the database already exists in the directory of the SD card, there is no need to recreate it, otherwise the file will be created And copy the database file if (! (new File (databaseFilename)) .exists ()) {/ res/raw database under / res/raw as the output stream InputStream is = this.context.getResources () .openRawResource (R.raw.zylz) / / data flow used to store database information FileOutputStream fos = new FileOutputStream (databaseFilename); byte [] buffer = new byte [8192]; int count = 0 / / write the data to while ((count = is.read (buffer)) > 0) {fos.write (buffer, 0, count) under the SD card directory } fos.flush (); fos.close (); is.close () }} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () }} catch (Exception e) {}
Just note that there is no res/raw when the project is set up, you just need to manually set up a raw folder in res
To access the database in the SD card, you need to write:
Package your.wzf.database.dal;import java.util.ArrayList;import java.util.List;import java.util.UUID;import your.wzf.database.dal.*;import your.wzf.database.model.Model_Bt_Farmer;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class Bt_Farmer {private String databasePath = android.os.Environment.getExternalStorageDirectory () .getAbsolutePath () + "/ nqxx_512022"; private String databaseFileName = "zylz.db" String fullPath = ""; public Bt_Farmer (Context context) {fullPath = databasePath + "/" + databaseFileName;} / * insert a record * @ param farmer Model_Bt_Farmer object * / public void Insert (Model_Bt_Farmer farmer) {SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase (fullPath, null) Db.execSQL ("insert into BT_FARMER (ID,NAME_,IDENTITY_CARD,NAME_PY) values (?,)", new Object [] {farmer.getId (), farmer.getName (), farmer.getIdCard (), farmer.getName_py ()}); db.close () }} after reading the above, do you have any further understanding of how to package sqlite to APK in android? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.