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

How to realize database operation in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to implement database operation in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to operate the database in Android.

Android database operation 1. Create a database

Android provides a standard way to create databases. Inherit SQLiteOpenHelper and implement onCreate and onUpgrade. One advantage is that it is easy to upgrade the database version.

Private static class DatabaseHelper extends SQLiteOpenHelper {DatabaseHelper (Context context) {super (context, DATABASE_NAME, null, DATABASE_VERSION);} @ Override public void onCreate (SQLiteDatabase db) {} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {}}

Android database operation 2. Operate the database

I have always wondered whether a function should shut down the database immediately after opening the database. Or does an application open the database only once and then close the database when the application exits? The concerns are performance and multithreading. Think of a method that takes the database operation class as a single piece and sets the lock of the database to false. Close the lock., of the database and add the corresponding Table lock to each Table modification.

Public class WebViewDatabase {public static synchronized WebViewDatabase getInstance (Context context) {/ / use per table Mutex lock, turn off database lock, this / / improves performance as database's ReentrantLock is expansive mDatabase.setLockingEnabled (false);} / / synchronize locks private final Object mHttpAuthLock = new Object (); public boolean hasHttpAuthUsernamePassword () {synchronized (mHttpAuthLock) {return hasEntries (TABLE_HTTPAUTH_ID);}

Come to think of it, the underlying data will be encapsulated into ContentProvider that can be called by the application. The standard practice is that if you are not familiar with ContentProvider, it will be a bit troublesome.

Specific Application Mechanism of Android Digital Certificate

Android data storage access mechanism

Android optional API scope

The correct way the Android debugger works

Introduction to the Application of Android Root File system

Android database operation 3. Data display

As Cursor said earlier, it is a random iterator that points to the data source to display the data. To bind a View to a Cursor, you usually set several parameters. One is the style of each line, called Row Layout, which is actually a normal Layout XML file. There is also a correspondence between a column and a real control. That control displays the value of which column, which needs to be configured. In order to customize a good data display control, the simplest you can customize a very PP Row Layout, the more complicated is that you can overload the binding control View, or adapter ListAdapter.

To use Cursor to dynamically bind View, each table has a _ id column.

Rebind Cursor and refresh the page

Cursor.requery (). Adapter. NotifyDataSetChanged ()

Think of a question, when the amount of data is very large, will there be insufficient memory? Cursor is a dynamic binding View. Taking a closer look at the code of android, CursorWindow provides Buffer internally to copy the data from the database to the Buffer. As a buffer displayed by View, its size is limited. Repopulate Buffer according to the change of V iew.

Android database operation 4. Export database

We need to check the database content of the phone, export it from the phone every time, and then check it with the sqlite tool. In fact, use adb shell sqlite to directly view the mobile phone database, here to attract jade, aspiring people use it as a tool to encapsulate sqlite statements, you can directly operate the mobile phone database.

At this point, I believe you have a deeper understanding of "how to achieve database operation in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report