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

What is the use of SQLite in Android

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

Share

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

This article mainly introduces "what is the use of SQLite in Android". In daily operation, I believe that many people have doubts about the use of SQLite in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "what is the use of SQLite in Android?" Next, please follow the editor to study!

There is a database built into the Android system, and that is SQLite. SQlite is a lightweight, embedded relational database

Its operation speed is very fast, takes up very few resources, and usually only needs a few hundred KB of memory, so it is especially suitable for use on mobile devices. SQLite not only supports the standard SQL syntax but also follows the ACID transactions of the database. It is much faster than the general database, and it can be used without even setting users and passwords. It is precisely because Android embeds this function and its powerful database into the system that local persistence has a qualitative leap.

Android provides an abstract class SQLiteOpenHelper, which inherits from this class, and implements onCreate and onUpgrade to create a database.

OnCreate is called when the database is created, and onUpgrade is called when the database is upgraded

First create a class that inherits SQLiteOpenHelper

Public class MySQLiteHelper extends SQLiteOpenHelper {private static String CREATE_TABLE_USER= "create table users (" + "id integer primary key autoincrement," + "userid text,password text); private Context sContext; public MySQLiteHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {super (context, name, factory, version); sContext=context } @ Override public void onCreate (SQLiteDatabase db) {db.execSQL (CREATE_TABLE_USER); Toast.makeText (sContext, "data Table created successfully", Toast.LENGTH_LONG). Show ();} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {}}

In MainActivity

Public class MainActivity extends AppCompatActivity {private MySQLiteHelper sqLiteHelper; private SQLiteDatabase myDb; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Button btCreateDb= (Button) findViewById (R.id.btCreateDb) BtCreateDb.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {sqLiteHelper=new MySQLiteHelper (MainActivity.this, "usersdb.db", null,1); myDb=sqLiteHelper.getWritableDatabase ();})

The creation can be completed

In fact, the so-called upgrade database means that if the version number of SQLiteOpenHelper is higher than the current number, you need to upgrade onUpgrade. If it is smaller than the current number, you need onDowngrade.

Public class MySQLiteHelper extends SQLiteOpenHelper {private static String CREATE_TABLE_USER= "create table users (" + "id integer primary key autoincrement," + "userid text,password text)"; private static String CREATE_TABLE_TYPE= "create table types (" + "id integer primary key autoincrement," + "type_code,describe text)"; private Context sContext Public MySQLiteHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {super (context, name, factory, version); sContext=context;} @ Override public void onCreate (SQLiteDatabase db) {db.execSQL (CREATE_TABLE_USER); db.execSQL (CREATE_TABLE_TYPE) Toast.makeText (sContext, "successfully create datasheet", Toast.LENGTH_LONG). Show ();} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL ("drop table if exists users"); db.execSQL ("drop table if exists types"); onCreate (db);}}

Add data

Insert (String table,String nullColumnHack,ContentValus values)

Update data

Update (String table,ContenValues values,String whereClause,String where [] Args)

Delete data

Delete (String table,String whereClause,String [] Args)

Query data

Query (String table,String [] columns,String selection,String [] selectionArgs,String groupBy,String having,String ordeBy,String limit)

You can also use the SQL command to manipulate the database, for example:

MyDb.execSQL (inser into users (userid,password) valus, new String [] {name,password}); at this point, the study on "what is the use of SQLite in Android" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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