文章目录
- 一、SQLite数据库管理工具
-
- 1、下载sqlite3
- 2、解压到指定目录
- 3、使用sqlite3操作数据库及表
- 4、使用数据可视化管理工具 - Navicat
-
- (1)打开Navicat工具
- (2)新建连接 - 选择SQLite
- (3)输入连接名
- (4)添加数据库文件
- (5)点击确定
- (6)打开学生表
- (7)新建查询,进行查询操作
- 二、案例演示 - 使用SQLiteDatabase类操作数据库
-
- 1、创建安卓应用
- 2、添加背景图片
- 3、字符串资源文件 - strings.xml
- 4、主布局资源文件 - activity_main.xml
- 5、主界面类 - MainActivity
- 6、运行程序,查看效果
- 三、案例演示 - 使用SQLiteDatabase类操作数据表
-
- 1、创建安卓应用
- 2、添加背景图片
- 3、字符串资源文件 - strings.xml
- 4、主布局资源文件 - activity_main.xml
- 5、主界面类 - MainActivity
- 6、运行程序,查看效果
- 四、案例演示 - 手机联系人(采用SQLite数据库)
-
- 1、创建安卓应用
- 2、添加背景图片
- 3、主布局资源文件 - activity_main.xml
- 4、自定义数据库助手类
- 5、主界面类 - MainActivity
- 6、运行程序,查看效果
一、SQLite数据库管理工具
1、下载sqlite3
- 下载网址: http://www.sqlite.org/download.html
2、解压到指定目录
- 修改目录名为 -
sqlite3
3、使用sqlite3操作数据库及表
- 打开命令提示符,进入 sqlite3目录
- 输入命令,创建数据库 - student.db(如果数据库存在,则会打开数据库)
- 查看全部数据库
- 创建学生表 - stu
- 查看表结构
- 插入表记录
- 查看表记录
- 更新表记录 - 将“戴沐白”改为“奥斯卡”’
- 删除记录 - 删除id为2的记录
- 删除学生表
- 帮助命令
- 重新创建学生表并插入记录
4、使用数据可视化管理工具 - Navicat
(1)打开Navicat工具
(2)新建连接 - 选择SQLite
(3)输入连接名
(4)添加数据库文件
(5)点击确定
(6)打开学生表
(7)新建查询,进行查询操作
二、案例演示 - 使用SQLiteDatabase类操作数据库
1、创建安卓应用
2、添加背景图片
3、字符串资源文件 - strings.xml
<resources>
<string name="app_name">创建和删除数据库string>
<string name="create_db">创建数据库string>
<string name="delete_db">删除数据库string>
resources>
4、主布局资源文件 - activity_main.xml
<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、主界面类 - MainActivity
package net.zs.create_dalete_db;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String DB_NAME_PREFIX = "student";
private static final int MODE = Context.MODE_PRIVATE;
private int count;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count = databaseList().length;
}
public void doCreateDB(View view) {
count++;
String dbname = DB_NAME_PREFIX + count + ".db";
try {
db = openOrCreateDatabase(dbname, MODE, null);
Toast.makeText(this, "恭喜,数据库【" + dbname + "】创建成功!", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "遗憾,数据库【" + dbname + "】创建失败!", Toast.LENGTH_SHORT).show();
}
}
public void doDeleteDB(View view) {
String[] dbnames = databaseList();
if (dbnames.length > 0) {
for (int i = 0; i < dbnames.length; i++) {
deleteDatabase(dbnames[i]);
}
count = 0;
Toast.makeText(this, "恭喜,数据库全部被删除!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "遗憾,没有数据库可删除!", Toast.LENGTH_SHORT).show();
}
}
}
6、运行程序,查看效果
三、案例演示 - 使用SQLiteDatabase类操作数据表
1、创建安卓应用
2、添加背景图片
3、字符串资源文件 - strings.xml
<resources>
<string name="app_name">利用SQLiteDatabase操作数据表string>
<string name="create_or_open_db">创建或打开数据库string>
<string name="create_table">创建表string>
<string name="add_record">添加表记录string>
<string name="update_record">更新表记录string>
<string name="display_all_records">显示全部表记录string>
<string name="delete_all_records">删除全部表记录string>
<string name="delete_table">删除表string>
<string name="delete_db">删除数据库string>
resources>
4、主布局资源文件 - activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<Button
android:id="@+id/btnCreateOrOpenDB"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doCreateOrOpenDB"
android:text="@string/create_or_open_db" />
<Button
android:id="@+id/btnCreateTable"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doCreateTable"
android:text="@string/create_table" />
<Button
android:id="@+id/btnAddRecord"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doAddRecord"
android:text="@string/add_record" />
<Button
android:id="@+id/btnUpdateRecord"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doUpdateRecord"
android:text="@string/update_record" />
<Button
android:id="@+id/btnDisplayAllRecords"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doDisplayAllRecords"
android:text="@string/display_all_records" />
<Button
android:id="@+id/btnDeleteAllRecords"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doDeleteAllRecords"
android:text="@string/delete_all_records" />
<Button
android:id="@+id/btnDeleteTable"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doDeleteTable"
android:text="@string/delete_table" />
<Button
android:id="@+id/btnDeleteDB"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:onClick="doDeleteDB"
android:text="@string/delete_db" />
LinearLayout>
5、主界面类 - MainActivity
package net.zs.operate_table;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String DB_NAME = "student.db";
private final String TABLE_NAME = "student";
private final int MODE = Context.MODE_PRIVATE;
private SQLiteDatabase db;
private int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doCreateOrOpenDB(View view) {
if (databaseList().length == 0) {
db = openOrCreateDatabase(DB_NAME, MODE, null);
Toast.makeText(this, "恭喜,数据库【" + DB_NAME + "】创建成功!", Toast.LENGTH_SHORT).show();
} else {
db = openOrCreateDatabase(DB_NAME, MODE, null);
Toast.makeText(this, "恭喜,数据库【" + DB_NAME + "】打开成功!", Toast.LENGTH_SHORT).show();
}
}
public void doCreateTable(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
Toast.makeText(this, "表【" + TABLE_NAME + "】已经存在!", Toast.LENGTH_SHORT).show();
} else {
try {
String strSQL = "CREATE TABLE " + TABLE_NAME + "(id integer, name text, gender text)";
db.execSQL(strSQL);
Toast.makeText(this, "创建表成功!", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(this, "创建表失败!", Toast.LENGTH_SHORT).show();
}
}
}
}
private boolean isTableExisted(String tableName) {
String strSQL = "SELECT * FROM sqlite_master WHERE type = ? AND name = ?";
Cursor cursor = db.rawQuery(strSQL, new String[]{"table", tableName});
if (cursor.getCount() > 0) {
return true;
} else {
return false;
}
}
public void doAddRecord(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
id = getNewId(TABLE_NAME);
ContentValues values = new ContentValues();
values.put("id", id);
values.put("name", "学生" + id);
values.put("gender", id % 2 == 1 ? "男" : "女");
long count = db.insert(TABLE_NAME, null, values);
if (count != -1) {
Toast.makeText(this, "恭喜,表记录添加成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "遗憾, 表记录添加失败!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "表【" + TABLE_NAME + "】不存在,请先创建!", Toast.LENGTH_SHORT).show();
}
}
}
private int getNewId(String tableName) {
if (isTableExisted(tableName)) {
Cursor cursor = db.query(tableName, null, null, null, null, null, null);
if (cursor.moveToLast()) {
int id = cursor.getInt(0);
return id + 1;
}
}
return 1;
}
public void doUpdateRecord(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
if (getRecordCount(TABLE_NAME) > 0) {
String strSQL = "UPDATE " + TABLE_NAME + " SET name = ?, gender = ? WHERE id = ?";
try {
db.execSQL(strSQL, new Object[]{"张晓芸", "女", 1});
Toast.makeText(this, "恭喜,表记录更新成功!", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(this, "遗憾,表记录更新失败!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "没有表记录可更新,请先添加表记录!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "表【" + TABLE_NAME + "】不存在,请先创建!", Toast.LENGTH_SHORT).show();
}
}
}
private int getRecordCount(String table_name) {
String strSQL = "SELECT * FROM " + table_name;
Cursor cursor = db.rawQuery(strSQL, null);
return cursor.getCount();
}
public void doDisplayAllRecords(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
if (getRecordCount(TABLE_NAME) > 0) {
String strSQL = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()) {
builder.append(cursor.getInt(0) + " " + cursor.getString(1) + " " + cursor.getString(2) + "\n");
}
Toast.makeText(this, "全部表记录\n\n" + builder.toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "没有表记录可显示,请先添加表记录!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "表【" + TABLE_NAME + "】不存在,请先创建!", Toast.LENGTH_SHORT).show();
}
}
}
public void doDeleteAllRecords(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
if (getRecordCount(TABLE_NAME) > 0) {
String strSQL = "DELETE FROM " + TABLE_NAME;
try {
db.execSQL(strSQL);
Toast.makeText(this, "全部表记录已删除!", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(this, "删除表记录失败!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "没有表记录可删除,请先添加记录!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "表【" + TABLE_NAME + "】不存在,请先创建!", Toast.LENGTH_SHORT).show();
}
}
}
public void doDeleteTable(View view) {
if (db == null) {
if (databaseList().length == 0) {
Toast.makeText(this, "请创建数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "请打开数据库【" + DB_NAME + "】。", Toast.LENGTH_SHORT).show();
}
} else {
if (isTableExisted(TABLE_NAME)) {
String strSQL = "DROP TABLE " + TABLE_NAME;
try {
db.execSQL(strSQL);
Toast.makeText(this, "表删除成功!", Toast.LENGTH_SHORT).show();
} catch (SQLException e) {
Toast.makeText(this, "表删除失败!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "表【" + TABLE_NAME + "】不存在,请先创建!", Toast.LENGTH_SHORT).show();
}
}
}
public void doDeleteDB(View view) {
if (databaseList().length == 0) {
Toast.makeText(this, "没有数据库可删除!", Toast.LENGTH_SHORT).show();
} else {
if (deleteDatabase(DB_NAME)) {
Toast.makeText(this, "数据库【" + DB_NAME + "】删除成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "数据库【" + DB_NAME + "】删除失败!", Toast.LENGTH_SHORT).show();
}
}
}
}
6、运行程序,查看效果
四、案例演示 - 手机联系人(采用SQLite数据库)
1、创建安卓应用
2、添加背景图片
3、主布局资源文件 - activity_main.xml
<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:orientation="vertical"
android:padding="15dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/lvContact"
android:layout_width="match_parent"
android:layout_height="wrap_content">
ListView>
LinearLayout>
4、自定义数据库助手类
- 创建 -
DBHelper
类
- 源代码
package net.zs.sqlite_contacts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
private Context context;
private static final String DB_NAME = "contact.db";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "contact";
public DBHelper(@Nullable Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTable(db, TABLE_NAME);
addRecords(db, TABLE_NAME);
}
private void createTable(SQLiteDatabase db, String tableName) {
String strSQL = "create table " + tableName + "(_id text, name text, gender text, age integer, telephone text)";
try {
db.execSQL(strSQL);
} catch (SQLException e) {
Toast.makeText(context, "遗憾,建表失败!", Toast.LENGTH_SHORT).show();
}
}
private void addRecords(SQLiteDatabase db, String tableName) {
ContentValues values = new ContentValues();
values.put("_id", "2020001");
values.put("name", "李小文");
values.put("gender", "男");
values.put("age", 19);
values.put("telephone", "15878786780");
db.insert(tableName, null, values);
values.put("_id", "2020002");
values.put("name", "韩玉玲");
values.put("gender", "女");
values.put("age", 20);
values.put("telephone", "15845678907");
db.insert(tableName, null, values);
values.put("_id", "2020003");
values.put("name", "唐语涵");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "13978789089");
db.insert(tableName, null, values);
values.put("_id", "2020004");
values.put("name", "董翔宇");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "15823234567");
db.insert(tableName, null, values);
values.put("_id", "2020005");
values.put("name", "郑文佳");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "13978781234");
db.insert(tableName, null, values);
values.put("_id", "2020006");
values.put("name", "伍子胥");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "15878786780");
db.insert(tableName, null, values);
values.put("_id", "2020007");
values.put("name", "南怀瑾");
values.put("gender", "男");
values.put("age", 18);
values.put("telephone", "13956567862");
db.insert(tableName, null, values);
values.put("_id", "2020008");
values.put("name", "李文华");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "15956567845");
db.insert(tableName, null, values);
values.put("_id", "2020009");
values.put("name", "吴文渊");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "13978784560");
db.insert(tableName, null, values);
values.put("_id", "2020010");
values.put("name", "陈燕文");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "15890903456");
db.insert(tableName, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor query(String tableName, String[] columns, String selection, String[] selectionArgs) {
SQLiteDatabase db = getReadableDatabase();
return db.query(tableName, columns, selection, selectionArgs, null, null, null, null);
}
public int delete(String tableName, String whereClause, String[] whereArgs) {
SQLiteDatabase db = getWritableDatabase();
return db.delete(tableName, whereClause, whereArgs);
}
public int update(String tableName, ContentValues values, String whereClause, String[] whereArgs) {
SQLiteDatabase db = getWritableDatabase();
return db.update(tableName, values, whereClause, whereArgs);
}
public long insert(String tableName, ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
return db.insert(tableName, null, values);
}
}
5、主界面类 - MainActivity
package net.zs.sqlite_contacts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
private Context context;
private static final String DB_NAME = "contact.db";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "contact";
public DBHelper(@Nullable Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTable(db, TABLE_NAME);
addRecords(db, TABLE_NAME);
}
private void createTable(SQLiteDatabase db, String tableName) {
String strSQL = "create table " + tableName + "(_id text, name text, gender text, age integer, telephone text)";
try {
db.execSQL(strSQL);
} catch (SQLException e) {
Toast.makeText(context, "遗憾,建表失败!", Toast.LENGTH_SHORT).show();
}
}
private void addRecords(SQLiteDatabase db, String tableName) {
ContentValues values = new ContentValues();
values.put("_id", "2020001");
values.put("name", "李小文");
values.put("gender", "男");
values.put("age", 19);
values.put("telephone", "15878786780");
db.insert(tableName, null, values);
values.put("_id", "2020002");
values.put("name", "韩玉玲");
values.put("gender", "女");
values.put("age", 20);
values.put("telephone", "15845678907");
db.insert(tableName, null, values);
values.put("_id", "2020003");
values.put("name", "唐语涵");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "13978789089");
db.insert(tableName, null, values);
values.put("_id", "2020004");
values.put("name", "董翔宇");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "15823234567");
db.insert(tableName, null, values);
values.put("_id", "2020005");
values.put("name", "郑文佳");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "13978781234");
db.insert(tableName, null, values);
values.put("_id", "2020006");
values.put("name", "伍子胥");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "15878786780");
db.insert(tableName, null, values);
values.put("_id", "2020007");
values.put("name", "南怀瑾");
values.put("gender", "男");
values.put("age", 18);
values.put("telephone", "13956567862");
db.insert(tableName, null, values);
values.put("_id", "2020008");
values.put("name", "李文华");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "15956567845");
db.insert(tableName, null, values);
values.put("_id", "2020009");
values.put("name", "吴文渊");
values.put("gender", "男");
values.put("age", 20);
values.put("telephone", "13978784560");
db.insert(tableName, null, values);
values.put("_id", "2020010");
values.put("name", "陈燕文");
values.put("gender", "女");
values.put("age", 18);
values.put("telephone", "15890903456");
db.insert(tableName, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor query(String tableName, String[] columns, String selection, String[] selectionArgs) {
SQLiteDatabase db = getReadableDatabase();
return db.query(tableName, columns, selection, selectionArgs, null, null, null, null);
}
public int delete(String tableName, String whereClause, String[] whereArgs) {
SQLiteDatabase db = getWritableDatabase();
return db.delete(tableName, whereClause, whereArgs);
}
public int update(String tableName, ContentValues values, String whereClause, String[] whereArgs) {
SQLiteDatabase db = getWritableDatabase();
return db.update(tableName, values, whereClause, whereArgs);
}
public long insert(String tableName, ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
return db.insert(tableName, null, values);
}
}
6、运行程序,查看效果