Android Study Notes 6.2 - SQLite Database

Table of contents

Zero, learning objectives

1. Overview of SQLite database

1. SQLite composition

2. SQLite data type

3. SQLite database features

2. Use the SQLiteDatabase class to operate the database

(1) Implementation steps

1. Create Android application [CreateDeleteDB]

2. Copy the background image to the drawable directory 

 3. String resource file strings.xml

 4. Main layout resource file activity_main.xml

 5. Main interface class - MainActivity

 6. Start the application and check the effect


Zero, learning objectives

  • Learn to use sqlite3 to create and operate databases
  • Able to use SQLiteDatabase to operate databases and tables
  • Able to use SQLiteOpenHelper to operate databases and tables


1. Overview of SQLite database

  • SQLite is an open source embedded database engine written in C language by D. Richard Hipp. It supports most SQL92 standards and runs on all major operating systems.

1. SQLite composition

  • SQLite consists of SQL compiler, kernel, backend and accessories. SQLite makes debugging, modifying and extending the SQLite kernel more convenient by utilizing a virtual machine and virtual database engine (VDBE). All SQL statements are compiled into an easy-to-read assembly that can be executed in the SQLite virtual machine. The overall structure diagram of SQLite is as follows:

2. SQLite data type

  • In summary, SQLite supports NULL, INTEGER, REAL, TEXT and BLOB data types, which respectively represent null values, integer values, floating point values, string literals, and binary objects.

3. SQLite database features

  • It is worth mentioning that the pocket-sized SQLite can support databases up to 2TB in size. Each database exists in the form of a single file, and these data are stored on the disk in the form of a B-Tree data structure.
  • In terms of transaction processing, SQLite implements independent transaction processing through exclusive and shared locks at the database level. This means that multiple processes can read data from the same database at the same time, but only one can write data. Before a process or thread can perform a write operation to the database, it must obtain an exclusive lock. After acquiring the exclusive lock, no other read or write operations will occur.
  • SQLite uses dynamic data types. When a value is inserted into the database, SQLite will check its type. If the type does not match the associated column, SQLite will try to convert the value to the type of the column. If it cannot be converted , the value will be stored as its own type, which SQLite calls "weakly typed". But there is a special case. If it is INTEGER PRIMARY KEY, other types will not be converted and a "datatype missmatch" error will be reported.
  • ACID, an abbreviation for the four basic elements required for correct execution of database transactions. Includes: Atomicity, Consistency, Isolation, and Durability. A system that supports transactions must have these four characteristics. Otherwise, the accuracy of data cannot be guaranteed during transaction processing, and the transaction process is likely to fail to meet the requirements of the transaction party.

2. Use the SQLiteDatabase class to operate the database

(1) Implementation steps

1. Create Android application [CreateDeleteDB]

2. Copy the background image to the drawable directory 

 3. String resource file strings.xml

<resources>
    <string name="app_name">创建和删除数据库</string>
    <string name="create_db">创建数据库</string>
    <string name="delete_db">删除数据库</string>
</resources>

 4. Main layout resource file activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnCreateDB"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="doCreateDB"
        android:text="@string/create_db"
        android:textSize="20dp" />

    <Button
        android:id="@+id/btnDeleteDB"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="doDeleteDB"
        android:text="@string/delete_db"
        android:textSize="20dp" />
</LinearLayout>

 5. Main interface class - MainActivity

 

  • Declare variables and constants 
  • Get the current number of databases in the application 
  • Write code to create database 
  •  Write code to delete database

 6. Start the application and check the effect

6.2 Material

Guess you like

Origin blog.csdn.net/justin02191004/article/details/128281907