irpas技术客

Android实现对SQLite数据库增删改查(学生管理系统项目)_Zheng_world!_android sqlite增删改查

irpas 5675

SQLite数据库的使用 上效果:一、介绍二、大致流程1.通过继承SQLiteOpenHelper抽象类,完成对数据库的创建2.进行Sqlite数据增删改查操作3.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()});//Integer.parseInt()是对字符串进行转换,转换为int类型 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=?";//查询字段名称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:

<?xml version="1.0" encoding="utf-8"?>

<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" /> </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" /> </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" /> </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" /> </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" /> </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 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 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" /> </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" /> </LinearLayout> 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))"; //创建一个表,表的名称叫user这里面会有四个字段,Integer是整形,varchar数据量的大小是根据后面括号进行变化 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


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Android #SQlite增删改查 #age