本文主要是介绍SQLite案例1:利用SQLiteDatabase操作数据库与表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
SQLite案例1:利用SQLiteDatabase操作数据库与表
一、运行效果
二、实现步骤
1、创建安卓应用SQLiteDatabaseDemo
2、准备背景图片background.jpg,拷贝到res下的mipmap目录里
3、主布局资源文件activity_main.xml
<?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:background="@mipmap/background"android:orientation="vertical"android:gravity="center"android:padding="10px"tools:context="net.lm.ied.sqlitedatabase.MainActivity"><Button
android:id="@+id/btn_create_or_open_db"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doCreateOrOpenDB"android:text="@string/create_or_open_db"/><Button
android:id="@+id/btn_create_tale"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doCreateTable"android:text="@string/create_table"/><Button
android:id="@+id/btn_add_record"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doAddRecord"android:text="@string/add_record"/><Button
android:id="@+id/btn_update_record"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doUpdateRecord"android:text="@string/update_record" /><Button
android:id="@+id/btn_display_all_records"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doDisplayAllRecords"android:text="@string/display_all_records" /><Button
android:id="@+id/btn_delete_all_records"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doDeleteAllRecords"android:text="@string/delete_all_records" /><Button
android:id="@+id/btn_delete_table"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doDeleteTable"android:text="@string/delete_table" /><Button
android:id="@+id/btn_delete_db"android:layout_width="200dp"android:layout_height="wrap_content"android:onClick="doDeleteDB"android:text="@string/delete_db" /></LinearLayout>
4、字符串资源文件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><string name="action_settings">设置</string>
</resources>
5、主界面类MainActivity
(1)定义常量和变量
import android.app.Activity;
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 Activity {/***数据库名*/private final String DB_NAME = "student.db";/*** 表名*/private final String TABLE_NAME = "student";/*** 文件访问模式 私有模式*/private final int MODE = Context.MODE_PRIVATE;/*** SQLite数据库*/private SQLiteDatabase db;/*** 学号*/private int id;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//利用资源布局文件设置用户界面setContentView(R.layout.activity_main);}
}
(2)创建或打开数据库按钮单击事件处理代码
/*** 创建或打开数据库** @param view*/
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();}
}
运行程序,结果如下:
第一次单击【创建或打开数据库】按钮:
第二次单击【创建或打开数据库】按钮:
(3)创建判断表是否存在的方法isTableExisted(String tableName)
/*** 判断表是否存在** @param tableName* @return*/
private boolean isTableExisted(String tableName) {// 定义SQL字符串String strSQL = "SELECT * FROM sqlite_master WHERE type = ? AND name = ?";// 执行SQL查询,返回游标Cursor cursor = db.rawQuery(strSQL, new String[] {"table", tableName});// 判断游标里是否有记录return cursor.getCount() > 0;
}
(4)创建表按钮单击事件处理代码
/*** 创建表** @param view*/
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_LONG).show();} else {try {// 定义SQL字符串String strSQL = "CREATE TABLE " + TABLE_NAME + "(id integer, name text, gender text)";// 执行SQL语句db.execSQL(strSQL);// 提示用户创建表成功Toast.makeText(this, "创建表成功!", Toast.LENGTH_LONG).show();} catch (SQLException e) {// 提示用户创建表失败Toast.makeText(this, "创建表失败!", Toast.LENGTH_LONG).show();}}}
}
运行程序,结果如下:
不单击【创建或打开数据库】按钮,直接单击【创建表】按钮:
单击【创建或打开数据库】按钮之后,再单击【创建表】按钮:
此时,再单击【创建表】按钮:
github项目代码:(https://github.com/Liumce/SQLiteDatabase.git)
这篇关于SQLite案例1:利用SQLiteDatabase操作数据库与表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!