小型的、可嵌入、開源的關係型數據庫,效率高,無數據類型,支持事務操做,程序驅動。java
Integer、varchar(10)、float、double、char(10)、text
create table 表名(字段名稱 數據類型 約束, 字段類型,數據類型 約束...) create table person(_id Integer primary key, name varchar(10), age Integer not null)
### 2.2 刪除表android
drop table 表名 drop table person
### 2.3 插入數據sql
insert into 表名[字段,字段] values(值1,值2) insert into person(_id, age) values(1, 20) insert into values(2, "zs", 30)
### 2.4 修改數據數據庫
update 表名 set 字段=新值 where 修改的條件 update person set name="ls", age = 20, where _id = 1
delete from 表名 where 刪除的條件 delete from person from where _id = 2
select 字段名 from 表名 where 查詢條件 group by 分組的字段 having 篩選條件 order by 排序字段 select * from person; select _id, name from person select * from person where _id = 1 select * from person where _id <> 1 select * from person where _id = 1 and age > 18 select * from person where name like "%小%" select * from person where name like "_小%" select * from person where name is null select * from person where name age between 10 and 20 select * from person where age > 18 order by _id
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="18dp" android:paddingRight="18dp" android:paddingLeft="40dp" android:paddingTop="50dp" android:orientation="vertical" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="建立數據庫" android:onClick="createDb" android:background="@android:color/holo_blue_dark"/> <Button android:id="@+id/btn_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="插入數據" android:onClick="click" android:background="@android:color/holo_blue_dark" android:layout_marginTop="20dp"/> <Button android:id="@+id/btn_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改數據" android:onClick="click" android:layout_marginTop="20dp" android:background="@android:color/holo_blue_dark"/> <Button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刪除數據" android:onClick="click" android:background="@android:color/holo_blue_dark" android:layout_marginTop="20dp"/> <Button android:id="@+id/btn_insertApi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="插入數據API" android:onClick="onClick" android:background="@android:color/holo_blue_dark" android:layout_marginTop="20dp"/> <Button android:id="@+id/btn_updateApi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改數據API" android:onClick="onClick" android:background="@android:color/holo_blue_dark" android:layout_marginTop="20dp"/> </LinearLayout>
/** * 1. 提供了onCreate() onUpgrade()等建立數據庫更新數據庫的方法 * 2. 提供了獲取數據庫對象的函數 */ public class MySqlitHelper extends SQLiteOpenHelper { /** * 構造函數 * @param context 上下文對象 * @param name 當前建立數據庫的名稱 * @param factory 遊標工廠 * @param version 表示建立的數據庫的版本 >= 1 */ public MySqlitHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } public MySqlitHelper(Context context) { super(context, Constant.DATABASE_NAME, null, Constant.DATABASE_VERSION); } /** * 當數據庫建立時回調的函數 * @param db 數據庫對象 */ @Override public void onCreate(SQLiteDatabase db) { Log.i("tag", "--------onCreate--------"); String sql = "create table " + Constant.TABLE_NAME + "(" + Constant._ID + " Integer primary key, " + Constant.NAME + " varchar(10), " + Constant.AGE +" Integer)"; db.execSQL(sql); } /** * 當數據庫版本更新時回調的函數 * @param sqLiteDatabase 數據庫對象 * @param i 數據庫舊版本 * @param i1 數據庫新版本 */ @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } /** * 當數據庫打開時回調的函數 * @param db */ @Override public void onOpen(SQLiteDatabase db) { Log.i("tag:", "-----------onOpen-------------"); super.onOpen(db); } }
/** * 主要是對數據庫操做的工具類 */ public class DbManager { private static MySqlitHelper helper; public static MySqlitHelper getInstance(Context context) { if (helper == null) { helper = new MySqlitHelper(context); } return helper; } /** * 根據sql語句在數據庫中執行語句 * @param db 數據庫對象 * @param sql sql語句 */ public static void execSQL(SQLiteDatabase db, String sql) { if (db != null) { if (sql != null && !"".equals(sql)) { db.execSQL(sql); } } } }
public class Constant { public static final String DATABASE_NAME = "info.db"; public static final int DATABASE_VERSION = 1; public static final String TABLE_NAME = "person"; public static final String _ID = "_id"; public static final String NAME = "name"; public static final String AGE = "age"; }
package com.example.sqlitedemo; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private MySqlitHelper helper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); helper = DbManager.getInstance(this); } /** * 點擊按鈕建立數據庫 */ public void createDb(View view) { /** * 建立或打開數據庫,若是數據不存在則建立數據庫,若是數據存在則直接打開數據庫 * 默認狀況下,兩個函數都表示打開過着建立可讀可寫的數據庫對象,若是磁盤已滿或者是數據自己權限等狀況下 * getReadableDatabase()打開的是隻讀數據庫 */ SQLiteDatabase db = helper.getWritableDatabase(); } public void click(View view) { switch (view.getId()) { case R.id.btn_insert: SQLiteDatabase db = helper.getWritableDatabase(); String sql = "insert into "+Constant.TABLE_NAME+" values(1, 'zhangsan', 20)"; DbManager.execSQL(db, sql); String sql2 = "inert into "+Constant.TABLE_NAME+" values(2, 'lisi', 25)"; DbManager.execSQL(db, sql2); db.close(); break; case R.id.btn_update: db = helper.getWritableDatabase(); String updateSql = "update person set name = 'xiaoming' where _id = 1"; DbManager.execSQL(db, updateSql); db.close(); break; case R.id.btn_delete: db = helper.getWritableDatabase(); String delSql = "delete from person where _id = 2"; DbManager.execSQL(db, delSql); db.close(); break; } } public void onClick(View view) { SQLiteDatabase db = helper.getWritableDatabase(); switch (view.getId()) { case R.id.btn_insertApi: /** * insert(String table, String nullColumnHack, ContentValues values) */ ContentValues values = new ContentValues(); values.put(Constant._ID, 3); values.put(Constant.NAME, "張三"); values.put(Constant.AGE, 30); long result = db.insert(Constant.TABLE_NAME, null, values); if (result > 0) { Toast.makeText(this, "插入數據成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "插入數據失敗", Toast.LENGTH_SHORT).show(); } case R.id.btn_updateApi: /** * update(String table, ContentValues values, String whereClause, String[] whereArgs) * 返回值表示修改的條數 */ ContentValues cv = new ContentValues(); cv.put(Constant.NAME, "小木"); // 須要修改的字段名稱,修改後的字段值 int count = db.update(Constant.TABLE_NAME, cv, "_id=?", new String[]{"3"}); } } }