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 Android operates SQLite

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how Android operates SQLite. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Preface

SQLite is an in-process library that implements a self-sufficient, serverless, zero-configured, transactional SQL database engine. It is a zero-configuration database, which means that, unlike other databases, you do not need to configure it in the system. Like other databases, the SQLite engine is not a separate process and can be connected statically or dynamically according to the needs of the application. SQLite directly accesses its stored files.

Why use SQLite

A system that does not require a separate server process or operation (serverless).

SQLite does not need to be configured, which means that it does not require installation or administration.

A complete SQLite database is stored in a single cross-platform disk file.

SQLite is very small and lightweight, smaller than 400KiB when fully configured, and less than 250KiB when omitting optional feature configurations.

SQLite is self-sufficient, which means that no external dependencies are required.

SQLite transactions are fully ACID compatible, allowing safe access from multiple processes or threads.

SQLite supports the functionality of most query languages of the SQL92 (SQL2) standard.

SQLite is written in ANSI-C and provides a simple and easy-to-use API.

SQLite can be run in UNIX (Linux, Mac OS-X, Android, iOS) and Windows (Win32, WinCE, WinRT).

To sum up, lightweight compatible multi-terminals do not need to be configured without external dependencies and have easy-to-understand API security.

How to use it in Android

The SQLiteOpenHelper.java provided by Android is an abstract class. Then in Eclipse to create the Class class, you can choose this abstract class template, while in Android Studio, you must write a class to inherit it, according to the usual concise and easy to understand the class name, what we create is the MyDataBaseHelper.java or DataBaseHelper.java abbreviation DBHelper.java.

How to create a database in Android Studio

Right-click to create a package for database .java files, select new, and select New Class

Select enter the class name in the pop-up box

The rules of this business are dead, and it can only be like this:

Public class DBHelper extends SQLiteOpenHelper {}

You need to write a constructor of the DBHelper class with all the parameters in order to generate database objects:

/ / A constructor with all parameters, which is required. Both Eclipse and Android Studio have automatic filling function public DatabaseHelper (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version);}

Two abstract methods in the inherited abstract class SQLiteOpenHelper:

@ Override public void onCreate (SQLiteDatabase db) {/ / create database sql statement String sql = "create table user (name varchar (20))"; / / execute sql statement db.execSQL (sql);} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {} Eclipse

Right-click to create a package for database .java files, select new, and select New Class

Select SQLiteOpenHelper in the pop-up box

Click Finish to create successfully.

How to generate a database

In the first line of code, the Activity is automatically generated when it is created. Create the database in Activity. Here we name the database "info.db", and the database version number is 1. The code is as follows:

/ / rely on DatabaseHelper's constructor with all parameters to create the database DBHelper dbHelper = new DBHelper (MainActivity.this, "info.db", null,1); SQLiteDatabase db = dbHelper.getWritableDatabase ()

Addition, deletion, modification and query of the database

Increase

Db.insert ()

Delete

Db.delete ()

Change

Db.update ()

Check

Cursor cursor = db.rawQuery ("select * from info.db", null); if (cursor.getCount ()! = 0) {} this is the end of the article on "how Android operates SQLite". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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