SQLite数据库的使用
上效果:
; 一、介绍
Sqlite是一种轻量级的数据库,它的设计目标是嵌入式的,很多嵌入式的设备使用到了它,它的占用资源非常的低,通常只需要几百K的内存就足够了
二、大致流程
1.通过继承SQLiteOpenHelper抽象类,完成对数据库的创建
2.进行Sqlite数据增删改查操作
常用的SQLite语句:
(id,age为字段,user为表名)
添加:insert into user(xx varchar(20),xxx Integer)values(?,?)
删除:delete from user where id=?
修改:update user set age=? where id=?
查询:select * from user where id=?
代码如下MainActivity:
package com.example.zheng.myapplication;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
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 ed_name,ed_class,ed_age,ed_ID,ed_select_number;
private Button btn_insert,btn_select,btn_update,btndelete;
private TextView findResult;
SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
SQLite sqLite=new SQLite(this);
database = sqLite.getWritableDatabase();
}
private void Init() {
ed_name=findViewById(R.id.ed_name);
ed_class=findViewById(R.id.ed_class);
ed_age=findViewById(R.id.ed_age);
ed_ID=findViewById(R.id.ed_number);
ed_select_number=findViewById(R.id.ed_select_number);
btn_insert=findViewById(R.id.btn_insert);
btn_select=findViewById(R.id.btn_select);
btn_update=findViewById(R.id.btn_update);
btndelete=findViewById(R.id.btn_delete);
findResult=findViewById(R.id.textView);
btn_insert.setOnClickListener(this);
btndelete.setOnClickListener(this);
btn_update.setOnClickListener(this);
btn_select.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String sql;
switch (view.getId())
{
case R.id.btn_insert:
sql="insert into user(name,class,age,number)values(?,?,?,?)";
database.execSQL(sql,new Object[]{ed_name.getText().toString(),ed_class.getText().toString(),Integer.parseInt(ed_age.getText().toString()),
ed_ID.getText().toString()});
Toast.makeText(this, "数据添加成功!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_delete:
sql="delete from user where number=?";
database.execSQL(sql,new Object[]{ed_ID.getText().toString()});
Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_select:
try{
findResult.setText("");
sql="select * from user where number=?";
Cursor cursor = database.rawQuery(sql, new String[]{ed_select_number.getText().toString()});
while(cursor.moveToNext())
{
String s_name=cursor.getString(cursor.getColumnIndex("name"));
String s_class=cursor.getString(cursor.getColumnIndex("class"));
int i_age=cursor.getInt(cursor.getColumnIndex("age"));
String s_ID=cursor.getString(cursor.getColumnIndex("number"));
findResult.setText("姓名:"+s_name+"\n"+"班级:"+s_class+"\n"+"年龄:"+i_age+"\n"+"编号:"+s_ID);
Toast.makeText(this, "查询成功!", Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
e.printStackTrace();
}
break;
case R.id.btn_update:
sql="update user set number=?,name=?,class=?,age=? where number=?";
database.execSQL(sql,new Object[]{ed_ID.getText().toString(),ed_name.getText().toString(),ed_class.getText().toString(),
Integer.parseInt(ed_age.getText().toString()),ed_select_number.getText().toString()});
Toast.makeText(this, "修改成功!", Toast.LENGTH_SHORT).show();
break;
}
}
}
代码如下activity_main:
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<textview android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="姓名:" android:textsize="25dp">
<edittext android:id="@+id/ed_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputtype="textPersonName" android:textsize="20dp">
</edittext></textview></linearlayout>
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<textview android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="班级:" android:textsize="25dp">
<edittext android:id="@+id/ed_class" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputtype="textPersonName" android:textsize="20dp">
</edittext></textview></linearlayout>
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<textview android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="年龄:" android:textsize="25dp">
<edittext android:id="@+id/ed_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputtype="textPersonName" android:textsize="20dp">
</edittext></textview></linearlayout>
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<textview android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.2" android:text="编号:" android:textsize="25dp">
<edittext android:id="@+id/ed_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputtype="textPersonName" android:textsize="20dp">
</edittext></textview></linearlayout>
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<textview android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="用户查询:" android:textsize="25dp">
<edittext android:id="@+id/ed_select_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:hint="请输入查询的编号" android:inputtype="textPersonName" android:textsize="20dp">
<button android:id="@+id/btn_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="查询" android:textsize="18dp">
</button></edittext></textview></linearlayout>
<linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.2" android:orientation="horizontal">
<button android:id="@+id/btn_insert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="添加" android:layout_margin="10dp" android:textsize="18dp">
</button><button android:id="@+id/btn_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" android:layout_margin="10dp" android:textsize="18dp">
</button><button android:id="@+id/btn_update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="修改" android:layout_margin="10dp" android:textsize="18dp">
</button></linearlayout>
<textview android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="XXXX" android:textcolor="#000" android:textsize="30dp">
</textview>
3.Sqlite类
package com.example.zheng.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLite extends SQLiteOpenHelper {
public SQLite(Context context) {
super(context, "SQL.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql="create table user(name varchar(30),class varchar(30),age Integer,number varchar(30))";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
总结:
android官方介绍网址:https://developer.android.google.cn/reference/androidx/sqlite/db/package-summary?hl=zh_cn
工程下载地址:
百度网盘 提取码:1111
GitHub:链接:https://github.com/HWJ-ZHENG/SQLite
Original: https://blog.csdn.net/weixin_54014031/article/details/121921501
Author: Zheng_world!
Title: Android实现对SQLite数据库增删改查(学生管理系统项目)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/815866/
转载文章受原作者版权保护。转载请注明原作者出处!