Android提供了SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQLiteDatabase对象,接下来可通过SQLiteDatabase对象来管理、操作数据库。
Android中使用SQLite数据库进行开发时,主要利用SQL语句来进行基本功能实现。掌握如下增删改查语句更利于学习。
创建表create table tableName(id integer primary key autoincrement , name , text , number text);
删除表 drop table if exists tableName;
增加一条数据:insert into tableName values(“小明”,”111”);
删除一条数据:delete from tableName where name=”小明”;
修改一条数据:update tableName set name=”小红” where name=”小明”;
查询一条数据:select*from tableName where name=”小明”;
首页
效果图:
MainActivity.java
package com.example.sqlite_foundation;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
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 insertData(View view){
//进行跳转
Intent intent = new Intent(this,insertActivity.class);
startActivity(intent);
}
public void deleteData(View view){
Intent intent = new Intent(this,DeleteActivity.class);
startActivity(intent);
}
public void updateData(View view){
Intent intent = new Intent(this,UpdateActivity.class);
startActivity(intent);
}
public void queryData(View view){
Intent intent = new Intent(this,QueryActivity.class);
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/back"
android:scaleType="matrix"
>
<Button
android:layout_width="300dp"
android:layout_height="60dp"
android:onClick="insertData"
android:text="添加数据"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:background="@color/yellow"/>
<Button
android:layout_width="300dp"
android:layout_height="60dp"
android:onClick="deleteData"
android:text="删除数据"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@color/yellow"/>
<Button
android:layout_width="300dp"
android:layout_height="60dp"
android:onClick="updateData"
android:text="修改数据"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@color/yellow"/>
<Button
android:layout_width="300dp"
android:layout_height="60dp"
android:onClick="queryData"
android:text="查询数据"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@color/yellow"/>
</LinearLayout>
数据库创建
MySQLiteOpenHerper.java
package com.example.sqlite_foundation;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import com.example.sqlite_foundation.bean.Student;
import java.util.ArrayList;
import java.util.List;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME="mySQLite.db";
private static final String TABLE_NAME_STUDENT="student";
//SQL语句准备
private static final String CREATE_TABLE_SQL = "create table " + TABLE_NAME_STUDENT + " (id integer primary key autoincrement,name text,number text,gender text,score text);";
//定死参数
public MySQLiteOpenHelper(Context context){
super(context,DB_NAME,null,1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//执行SQL语句
sqLiteDatabase.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
//插入数据方法
public long insertData(Student student){
//得到一个可写数据库
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
//传值准备数据
values.put("name",student.getName());
values.put("number",student.getNumber());
values.put("gender",student.getGender());
values.put("score",student.getScore());
//把数据插入到数据库中
//返回当前行的id,若为-1则数据插入失败
return db.insert(TABLE_NAME_STUDENT,null,values);
}
//按姓名删除数据方法
public int deleteFromDbByName(String name){
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_NAME_STUDENT,"name like ?",new String[]{name});
}
//修改数据方法
public int updateData(Student student){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
//传值准备数据
values.put("name",student.getName());
values.put("number",student.getNumber());
values.put("gender",student.getGender());
values.put