

标准的SQL语句分为三类:数据定义、数据操纵和数据控制,但不同的数据库往往有自己的实现。
SQLite是一种小巧的嵌入式数据库,由于它属于轻型数据库,不涉及复杂的数据控制操作,因此App开发只用到数据定义和数据操纵两类SQL。
SQLite的SQL语法与通用的SQL语法略有不同。



SQLiteDatabase是SQLite的数据库管理类,它提供了若干操作数据表的API,
常用的方法有3类:
1. 管理类,用于数据库层面的操作。
openDatabase:打开指定路径的数据库。
isOpen:判断数据库是否已打开。
close:关闭数据库。
getVersion:获取数据库的版本号。
setVersion:设置数据库的版本号。
2. 事务类,用于事务层面的操作。
beginTransaction:开始事务。
setTransactionSuccessful:设置事务的成功标志。
endTransaction:结束事务。
3. 数据处理类,用于数据表层面的操作。
execSQL:执行拼接好的SQL控制语句。
delete:删除符合条件的记录。
update:更新符合条件的记录。
insert:插入一条记录。
query:执行查询操作,返回结果集的游标。
rawQuery:执行拼接好的SQL查询语句,返回结果集的游标。
示例:
页面布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_database_create"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="创建数据库"
android:textColor="@color/black"
android:textSize="17sp" />
<Button
android:id="@+id/btn_database_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除数据库"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>
<TextView
android:id="@+id/tv_database"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textColor="@color/black"
android:textSize="17sp" />
</LinearLayout>

代码:
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class DatabaseActivity extends AppCompatActivity implements View.OnClickListener
{
private TextView tv_database;
private String mDatabaseName;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);
tv_database = findViewById(R.id.tv_database);
findViewById(R.id.btn_database_create).setOnClickListener(this);
findViewById(R.id.btn_database_delete).setOnClickListener(this);
// 生成一个测试数据库的完整路径
mDatabaseName = getFilesDir() + "/test.db";
}
@Override
public void onClick(View v)
{
if (v.getId() == R.id.btn_database_create)
{
// 创建或打开数据库。数据库如果不存在就创建它,如果存在就打开它
SQLiteDatabase db = openOrCreateDatabase(mDatabaseName, Context.MODE_PRIVATE, null);
String desc = String.format("数据库%s创建%s", db.getPath(), (db!=null)?"成功":"失败");
tv_database.setText(desc);
} else if (v.getId() == R.id.btn_database_delete)
{
boolean result = deleteDatabase(mDatabaseName); // 删除数据库
String desc = String.format("数据库%s删除%s", mDatabaseName, result?"成功":"失败");
tv_database.setText(desc);
}
}
}














=========================================================================================================================

SQLiteOpenHelper是Android提供的数据库辅助工具,用于指导开发者进行SQLite的合理使用。
SQLiteOpenHelper的具体使用步骤如下:
(1)新建一个继承自SQLiteOpenHelper的数据库操作类,提示重写onCreate和onUpgrade两个方法。
(2)封装保证数据库安全的必要方法,包括以下三种。
获取单例对象:确保App运行时数据库只被打开一次,避免重复打开引起错误。
打开数据库连接:读连接可调用SQLiteOpenHelper的getReadableDatabase方法获得,写连接可调用getWritableDatabase获得。
关闭数据库连接:数据库操作完了,调用SQLiteDatabase对象的close方法关闭连接。
(3)提供对表记录进行增加、删除、修改、查询的操作方法。



布局:
123
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/281269.html