The SQLite database adopts a modular design and consists of eight independent modules. These independent modules constitute three major subsystems. The module divides the complex Query Process into small tasks for processing.
There are about 30 thousand lines of Standard C code at its core. The modular design makes the code easier to understand.
The following figure shows the SQLite framework model:
Interface: It is composed of SQLite APIs. Therefore, whether it is an application, script, or library file, it eventually interacts with SQLite through the interface.
Virtual Machine: the most important part of the SQLite database architecture is the virtual machine, also known as the virtual database engine (vdbe), which is similar to the Java Virtual Machine. The virtual database engine is used to explain the execution of byte code, the byte code of the virtual database engine consists of 128 operation codes. These operation codes are mainly used to operate databases. Each command can perform specific database operations, or process stack content in a specific way
Some basic operations are as follows:
Public class mydata {context; sqlhelper; sqlitedatabase sqlitedatabasesd; Public mydata (context) {This. context = context;} public void open () {sqlhelper = new sqlhelper (context, "database. DB ", null, 2); try {sqlitedatabase = sqlhelper. getwritabledatabase ();} catch (exception e) {// todo auto-generated catch blocksqlitedatabase = sqlhelper. getreadableda Tabase () ;}/ ** SD card create database */Public void create () {string createdatabase = "create table student (SID integer primary key autoincrement, sname varchar not null, sage varchar not null, ssex varchar not null); "; file = new file ("/mnt/sdcrad/mydatabase. DP "); sqlitedatabase = sqlitedatabase. openorcreatedatabase (file, null); // sqlitedatabase = sqlitedatabase. opendatabase ("/mydatabase. DP ", null, context. moD E_world_readable + context. mode_world_writeable); // This sentence is the same as the two-sentence sqlitedatabase.exe csql ("Drop table if exists student" );sqlitedatabasesd.exe csql (createdatabase ); // The subsequent data operation does not use the created SD table}/** query data */Public void query () {cursor = sqlitedatabase. query ("student", // New String [] {"Sid", "sname", "Sage ", "ssex"} // data field to be queried, null); cursor. movetofirst (); // before moving to the first entry of the data result while (curs Or. movetonext () {string id = cursor. getstring (cursor. getcolumnindex ("_ id"); string name = cursor. getstring (cursor. getcolumnindex ("name"); string sex = cursor. getstring (cursor. getcolumnindex ("sex"); string age = cursor. getstring (cursor. getcolumnindex ("Age"); system. out. println ("ID" + ID + "name" + name + "sex" + sex + "Age" + age);} // display the data, you can also package it into a dataset and return it}/** insert data */Public void insert () {contentvalues CV = New contentvalues (); cv. put ("sname", "lonuery"); cv. put ("Sage", "23"); cv. put ("ssex", "male"); sqlitedatabase. insert ("student", null, CV);}/** delete data */Public void Delete () {sqlitedatabase. delete ("student", "deleted condition", new string [] {"10"});}/** update data */Public void updata () {contentvalues CV = new contentvalues (); cv. put ("Sage", "23"); sqlitedatabase. update ("student", CV, "condition to be updated", new string [] {"399"}); // wherecaurs E. When the condition of the data to be updated is null, update all data. // if the value of the data to be updated by whereargs is null, update all data.}/** close database */Public void close () {If (sqlhelper! = NULL) {sqlhelper. close (); sqlhelper = NULL;} class sqlhelper extends sqliteopenhelper {public sqlhelper (context, string name, cursorfactory factory, int version) {super (context, name, factory, version); // todo auto-generated constructor stub} string createtable = "create table student (SID integer primary key autoincrement, sname varchar not null, sage varchar not null, ssex varchar not null); "; @ overridepublic void oncreate (sqlitedatabase dB) {// todo auto-generated method stubdb.exe csql (createtable );} // create a database @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stubdb.exe csql ("Drop table if exists student" mongodb.exe csql (createtable);} // update the database, that is, delete or add data fields }}
How to Create a database using code:
1. The above sqlhelper class inherits the sqliteopenhelper class, and then reloads its constructor. It is a method to create a database for you when instantiating the sqlhelper class.
2. Use the openorcreatedatabase method of context or the openorcreatedatabase method of sqlitedatabase to create a database.
On the android forum, a user raised a very good question:
What is the difference between context. openorcreatedatabase and sqlitedatabase. openorcreatedatabase? Since sqlitedatabase can be used to create a database, why must sqliteopenhelper be used to create a database?
Some netizens gave a good answer:
Context. openorcreatedatabase and sqlitedatabase. openorcreatedatabase provide the same functions in essence. In the end, context. openorcreatedatabase needs to be called
Sqlitedatabase. openorcreatedatabase to create a database. That is to say, the sqlitedatabase class is the underlying encapsulation of SQLite on Android, and almost all
Library operations are ultimately implemented through this class. The method provided in context is used to create a database when the context is used. For example, the database you create in a logic is only in a specific context.
In, the database permissions are managed by context, and this logic may be provided to more than one context. As for sqlitedatabase and sqliteopenhelper, the latter is better understood.
It's just an abstract class that tells you how to use the sqlitedatabase class. You can write your own helper Based on sqlitedatabase.