Android 与SQlite 数据库操作(新手步骤)
- 一、搭建简易的界面
- 1 新建shape xml文件,定义一个圆角和边框
- 1.1 创建shape文件 tv_corner.xml
- 1.2 创建shape文件 tv_stroke.xml
- 2 搭建简易页面 activity_main.xml
- 二、新建MyHelper.java
- 1 构造方法,调用SQLiteOpenHelper的构造方法,创建数据库“contacter”
- 2 重写方法
- 三.搭建MainActivity.java框架
- 1.定义控件,创建点击事件框架
- 2.创建MyHelper 对象
- 3.实现增,改,删,查功能
- 3.1 插入功能
- 3.2 查询功能
- 3.3 更新功能
- 3.4 删除功能
- 3.5 button按钮点击事件(插入,查询,更新,删除)
- 四.整体代码
- 五.演示效果
- 六.表格显示数据(改进)
- 6.1 TableLayout替换原来的TextView
- 6.2 替换原来的查找数据代码
一、搭建简易的界面
1 新建shape xml文件,定义一个圆角和边框
1.1 创建shape文件 tv_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp"/>
<stroke android:color="@color/colorPrimaryDark"
android:width="3dp"/>
</shape>
1.2 创建shape文件 tv_stroke.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="3dp"/>
<stroke android:color="#28A62D"
android:width="3dp"/>
</shape>
2 搭建简易页面 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="简易通讯录"
android:textSize="40sp"
android:textColor="#1051DD"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="30sp" />
<EditText
android:id="@+id/ed_name"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="@drawable/tv_stroke"
android:hint="请输入用户名"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号:"
android:textSize="30sp"/>
<EditText
android:id="@+id/ed_phonenum"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:background="@drawable/tv_stroke"
android:hint="请输入手机号"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/bt_insert"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:textSize="20sp"
android:background="@drawable/tv_corner"
android:text="插入" />
2.1 界面截图
2.2 控件id截图
二、新建MyHelper.java
1 构造方法,调用SQLiteOpenHelper的构造方法,创建数据库“contacter”
public MyHelper(Context context){
super(context,"contacter",null,2);
}
2 重写方法
package com.maaa.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context){
super(context,"contacter.db",null,2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
三.搭建MainActivity.java框架
1.定义控件,创建点击事件框架
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText name,phonenum;
private Button insert,query,updata,delete;
private TextView show;
private void init(){
name = findViewById(R.id.ed_name);
phonenum = findViewById(R.id.ed_phonenum);
show = findViewById(R.id.tv_show);
insert= findViewById(R.id.bt_insert);
query = findViewById(R.id.bt_query);
updata = findViewById(R.id.bt_updata);
delete = findViewById(R.id.bt_delete);
insert.setOnClickListener(this);
query.setOnClickListener(this);
updata.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bt_insert:
break;
case R.id.bt_query:
break;
case R.id.bt_updata:
break;
case R.id.bt_delete:
break;
}
}
}
2.创建MyHelper 对象
MyHelper myHelper =new MyHelper(this);
3.实现增,改,删,查功能
3.1 插入功能
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
show.setText("数据"+name+"插入成功");
Toast.makeText(this,"插入成功",Toast.LENGTH_SHORT).show();
db.close();
break;
3.2 查询功能
Integer i =1;
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information", null, null, null, null, null, null);
if(cursor.getCount() == 0){
show.setText("没有数据");
}else {
cursor.moveToFirst();
show.setText("序号 姓名 电话");
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
while (cursor.moveToNext()){
i++;
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
cursor.close();
db.close();
break;
3.3 更新功能
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("phone",phone);
db.update("information",values,"name = ?",new String[]{name});
show.setText(name+"电话更新成功");
db.close();
break;
3.4 删除功能
name = edname.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
db.delete("information","name=?",new String[]{name});
show.setText(name+"删除成功");
db.close();
break;
3.5 button按钮点击事件(插入,查询,更新,删除)
@Override
public void onClick(View v) {
String name;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()){
case R.id.bt_insert://插入数据
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
show.setText("数据"+name+"插入成功");
Toast.makeText(this,"插入成功",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.bt_query://查找数据
Integer i =1;
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information", null, null, null, null, null, null);
if(cursor.getCount() == 0){
show.setText("没有数据");
}else {
cursor.moveToFirst();
show.setText("序号 姓名 电话");
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
while (cursor.moveToNext()){
i++;
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
cursor.close();
db.close();
break;
case R.id.bt_updata://更新数据
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("phone",phone);
db.update("information",values,"name = ?",new String[]{name});
show.setText(name+"电话更新成功");
db.close();
break;
case R.id.bt_delete://删除数据
name = edname.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
db.delete("information","name=?",new String[]{name});
show.setText(name+"删除成功");
db.close();
break;
}
}
四.整体代码
package com.maaa.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText edname,edphonenum;
private Button insert,query,updata,delete;
private TextView show;
MyHelper myHelper;
private void init(){
edname = findViewById(R.id.ed_name);
edphonenum = findViewById(R.id.ed_phonenum);
show = findViewById(R.id.tv_show);
insert= findViewById(R.id.bt_insert);
query = findViewById(R.id.bt_query);
updata = findViewById(R.id.bt_updata);
delete = findViewById(R.id.bt_delete);
insert.setOnClickListener(this);
query.setOnClickListener(this);
updata.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
myHelper =new MyHelper(this);
}
@Override
public void onClick(View v) {
String name;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()){
case R.id.bt_insert://插入数据
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
values.put("phone",phone);
db.insert("information",null,values);
show.setText("数据"+name+"插入成功");
Toast.makeText(this,"插入成功",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.bt_query://查找数据
Integer i =1;
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information", null, null, null, null, null, null);
if(cursor.getCount() == 0){
show.setText("没有数据");
}else {
cursor.moveToFirst();
show.setText("序号 姓名 电话");
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
while (cursor.moveToNext()){
i++;
show.append("\n"+i+" "+cursor.getString(cursor.getColumnIndex("name"))+" "+cursor.getString(2));
}
cursor.close();
db.close();
break;
case R.id.bt_updata://更新数据
name = edname.getText().toString().trim();
phone = edphonenum.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("phone",phone);
db.update("information",values,"name = ?",new String[]{name});
show.setText(name+"电话更新成功");
db.close();
break;
case R.id.bt_delete://删除数据
name = edname.getText().toString().trim();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
db.delete("information","name=?",new String[]{name});
show.setText(name+"删除成功");
db.close();
break;
}
}
}
五.演示效果
六.表格显示数据(改进)
6.1 TableLayout替换原来的TextView
TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="2"
>
</TableLayout>
<!-- <TextView-->
<!-- android:id="@+id/tv_show"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:textSize="20sp"-->
<!-- android:background="@drawable/tv_corner"-->
<!-- android:padding="10dp"-->
<!-- android:text="此处为显示内容区"/>-->
6.2 替换原来的查找数据代码
case R.id.bt_query://查找数据
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if (cursor.getCount() == 0){
show.setText("没有数据");
}else{
Integer row =cursor.getCount()+1;
Integer column = cursor.getColumnCount();
tableLayout.removeAllViews();
tableLayout.setStretchAllColumns(true);
TableRow tableRow = new TableRow(MainActivity.this);
tableRow.setGravity(Gravity.CENTER);
TextView tv1 =new TextView(MainActivity.this);
TextView tv2 =new TextView(MainActivity.this);
TextView tv3 =new TextView(MainActivity.this);
tv1.setText("序号");
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundResource(R.drawable.tv_stroke);
tv1.setTextSize(20);
tableRow.addView(tv1);
tv2.setText("姓名");
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundResource(R.drawable.tv_stroke);
tv2.setTextSize(20);
tableRow.addView(tv2);
tv3.setText("电话");
tv3.setGravity(Gravity.CENTER);
tv3.setBackgroundResource(R.drawable.tv_stroke);
tv3.setTextSize(20);
tableRow.addView(tv3);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1));
int i=1;
while (cursor.moveToNext()){
TableRow tableRow1 = new TableRow(MainActivity.this);
for (int j=0;j<cursor.getColumnCount();j++){
TextView tv =new TextView(MainActivity.this);
tv.setGravity(Gravity.CENTER);
tv.setBackgroundResource(R.drawable.tv_stroke);
tv.setTextSize(20);
if(j==0){
tv.setText(String.valueOf(i));
tableRow1.addView(tv);
}else{
tv.setText(cursor.getString(j));
tableRow1.addView(tv);
}
}
tableLayout.addView(tableRow1, new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,1));
i++;
}