Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Encapsulation of android local database sqlite

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Implementation of a stand-alone android sqlite database that can be generated with the program in the installation package

Download the sqlite3.exe file

Run cmd to the directory where sqlite3.exe is located and run the sqlite3.exe database name. db

Then a command prompt for sqlite > appears

Enter the statement to create the table, the name of the create table table (column, column. ); (note: add a semicolon at the end)

The files of the database you built will appear in the directory where the sqlite3.exe is located.

Third, if you want to run in Android, you need to add to the database

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'zh_CN') INSERT INTO "android_metadata" VALUES (' zh_CN') 4. Copy the database into res/raw in the Android project. 5. Here is the code: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123public class TestSqlDatabase {private static final String DATABASE_PATH = "/ data/data/your.package.name/databases"; / / do not change here, this is the physical address of the database on the phone private static final int DATABASE_VERSION = 0 Private static final String DATABASE_NAME = "test.db"; / / here is the database name private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME; private Context context; private SQLiteDatabase database; public TestSqlDatabase (Context context) {this.context = context; File file = new File (outFileName); if (file.exists ()) {database = SQLiteDatabase.openOrCreateDatabase (outFileName, null); if (database.getVersion ()! = DATABASE_VERSION) {database.close (); file.delete () }} try {buildDatabase ();} catch (Exception e) {e.printStackTrace ();}} private void buildDatabase () throws Exception {InputStream myInput = context.getResources () .openRawResource (R.raw.test); File file = new File (outFileName); File dir = new File (DATABASE_PATH); if (! dir.exists ()) {if (! dir.mkdir ()) {throw new Exception ("creation failed") }} if (! file.exists ()) {try {OutputStream myOutput = new FileOutputStream (outFileName); byte [] buffer = new byte [1024]; int length; while ((length = myInput.read (buffer)) > 0) {myOutput.write (buffer, 0, length);} myOutput.close (); myInput.close ();} catch (Exception e) {e.printStackTrace () } / * find * @ return * / public Cursor select () {database = SQLiteDatabase.openOrCreateDatabase (outFileName, null); String sql = "select * from note_table"; Cursor cursor = database.rawQuery (sql, null); return cursor;} / * * insert * @ param word * @ param note * @ return * / public long insert (String word, String note) {database = SQLiteDatabase.openOrCreateDatabase (outFileName, null); ContentValues cv = new ContentValues () Cv.put ("word", word); cv.put ("note", note); long result = database.insert ("note_table", null, cv); return result;} / * * Update * @ param word * @ param note * @ return * / private int update (String word, String note) {/ / Parameter word is modified if note is modified database = SQLiteDatabase.openOrCreateDatabase (outFileName, null); ContentValues cv = new ContentValues (); cv.put ("note", note) Int result = database.update ("note_table", cv, "word=?", new String [] {word}); return result;} / * * Delete * @ param word * / public int deleteNote (String word) {database = SQLiteDatabase.openOrCreateDatabase (outFileName, null); int result = database.delete ("note_table", "word=?", new String [] {word}); return result;} public void close () {database.close ();}}

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report