45. SQLite
45.1 SQLite介绍
SQLite关系型数据库。
嵌入式的数据库,体积小,功能强大,几十kb。
在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n)、char(n)、decimal(p,s)
等数据类型,只不过在运算或保存时会转成对应的五种数据类型。SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,但是主键只能是Integer类型的。
Sqlite数据库一般要求主键是_id,当然也可以是id.
原来:数据库
安装一个数据库的软件。
android里面的数据库是由底层的sqilte.c的代码来动态生成的。
45.2 SQLite可视化工具
笔者没用老师用的,我直接用Navicat 15
45.3 SQLite创库创表
Android系统封装了一个类 SqliteOpenHelper
新建工程
自定义类
package com.dingjiaxiong.mysqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static SQLiteOpenHelper mInstance;
public static synchronized SQLiteOpenHelper getmInstance(Context context){
if(mInstance == null){
mInstance = new MySQLiteOpenHelper(context , "dingjiaxiong.db",null,1);
}
return mInstance;
}
private MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//数据库初始化用的
@Override
public void onCreate(SQLiteDatabase db) {
}
//数据库升级用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
布局
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成DB文件"
android:onClick="createDB"
/>
</LinearLayout>
运行
这个表是默认生成的,无用。(意思是英文环境)
创建表
运行程序
45.4 SQLite增删改查
修改布局
<?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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="生成DB文件"
android:onClick="createDB"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="query"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入"
android:onClick="insert"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:onClick="update"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="delete"
/>
</LinearLayout>
package com.dingjiaxiong.mysqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createDB(View view) {
SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
//database文件夹 → 创建
SQLiteDatabase readableDatabase = helper.getReadableDatabase();
}
public void query(View view) {
SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getReadableDatabase();
if (db.isOpen()) {
Cursor cursor = db.rawQuery("select * from persons", null);
//迭代游标
while (cursor.moveToNext()) {
// int _id = cursor.getInt(0);
// @SuppressLint("Range") int _id = cursor.getInt(cursor.getColumnIndex("_id"));
// @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
int _id = cursor.getInt(0);
String name = cursor.getString(1);
Log.e("dingjiaxiong", "query: _id: " + _id + " name: " + name);
}
cursor.close();
db.close();
}
}
public void insert(View view) {
SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if (db.isOpen()) {
String sql = "insert into persons(name) values('dingjiaxiong')";
db.execSQL(sql);
db.close();
}
}
public void update(View view) {
SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if (db.isOpen()) {
String sql = "update persons set name = ? where _id = ?";
db.execSQL(sql,new Object[]{"凤凤",4});
db.close();
}
}
public void delete(View view) {
SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if (db.isOpen()) {
String sql = "delete from persons where _id = ?";
db.execSQL(sql,new Object[]{6});
db.close();
}
}
}
运行
标签:SQLite,layout,45,db,content,wrap,import,android From: https://www.cnblogs.com/55zjc/p/16706585.html