最近接了一个小项目,做一款记账本软件,主要涉及到SQLite,接下来是关于android中SQLite的使用以及实现方式。
创建SQLHelper类
首先我们需要创建一个DataBaseHelperSQ类,用来继承SQLiteOpenHelper类,继承SQLiteOpenHelper需要重写onCreate()方法以及onUpgrade()方法
- onCreate() :初始化数据库,创建表
- onUpgrade() :在数据表需要更新使用
同时,还有一个构造方法。
public class DataBaseHelperSQ extends SQLiteOpenHelper {
public DataBaseHelperSQ(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE user(userid integer primary key autoincrement," +
"username text not null," +
"userpwd text not null)";
db.execSQL(sql); // 初始化执行创建user表语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
创建界面
创建两个EditText控件和四个Button按钮,按钮用来获取EditRText控件的值来实现增删改查,需要为每一个控件创建一个ID
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SqlActivity">
<EditText
android:id="@+id/ed_sq_user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
/>
<EditText
android:id="@+id/ed_sq_pswd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
/>
<Button
android:id="@+id/btn_addsq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="增加数据"
android:onClick="btnSqOnclick"
/>
<Button
android:id="@+id/btn_delsq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据"
android:onClick="btnSqOnclick"
/>
<Button
android:id="@+id/btn_xgsq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改数据"
android:onClick="btnSqOnclick"
/>
<Button
android:id="@+id/btn_cxsq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据"
android:onClick="btnSqOnclick"
/>
</LinearLayout>
实现增删改查功能
在SqlActivity中创建数据库helper的对象,创建名为demo.db
的sqlite数据库文件,并且后缀名称应该为db
package com.kaygb.xiangduibuju;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class SqlActivity extends AppCompatActivity {
// 声明控件对象
EditText sqUser;
EditText sqPswd;
// 创建 SQLiteDatabase对象
SQLiteDatabase db;
// 创建 DataBaseHelperSQ对象
DataBaseHelperSQ helper;
// 全局变量
String sql=null;
String uname =null;
String upwd = null;
// 创建Cursor 数据库游标对象
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sql);
// 绑定控件
sqUser = findViewById(R.id.ed_sq_user);
sqPswd = findViewById(R.id.ed_sq_pswd);
// 创建名称为demo的sqlite数据库,版本号为1,
helper = new DataBaseHelperSQ(this,"demo.db",null,1);
// 实现可写数据
db = helper.getWritableDatabase();
}
// 按钮点击事件,实现增删改查
public void btnSqOnclick(View view){
uname = sqUser.getText().toString();
upwd = sqPswd.getText().toString();
switch (view.getId()){
case R.id.btn_addsq: //增加一条数据
sql = "insert into user(username,userpwd) values('"+uname+"','"+upwd+"')";
db.execSQL(sql);
sqUser.setText("");
sqPswd.setText("");
Toast.makeText(this,"创建成功",Toast.LENGTH_SHORT).show();
break;
case R.id.btn_delsq: // 删除
sql = "delete from user where username ='"+uname+"'";
Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
db.execSQL(sql);
sqUser.setText("");
sqPswd.setText("");
break;
case R.id.btn_xgsq: // 修改
sql = "update user set userpwd='"+upwd+"' where username ='"+uname+"'";
Toast.makeText(this,"修改成功",Toast.LENGTH_SHORT).show();
db.execSQL(sql);
sqUser.setText("");
sqPswd.setText("");
break;
case R.id.btn_cxsq: // 查询
String[] args = null;
sql = "select * from user";
if(uname!=null && !uname.equals("")){
if(!uname.equals("all")){
sql = sql + " where username=?";
args = new String[]{uname};
if (upwd !=null && !upwd.equals("")){
sql = sql + " and userpwd=?";
args = new String[]{uname,upwd};
}
}else {
args=null;
}
cursor = db.rawQuery(sql,args);// 为db创建游标指针
cursor.moveToFirst(); // 将游标指针指向第一条数据
while (!cursor.isAfterLast()){ // 判断游标是否在最后一条数据,如果不是则向下移动一位
// 获取游标所指向当前数据表行的username的值
String name = cursor.getString(cursor.getColumnIndex("username"));
// 获取游标所指向当前行的userpwd的值
String pwd = cursor.getString(cursor.getColumnIndex("userpwd"));
//在logcat输出数据库中的所有信息
Log.e("DATABASE","用户名:"+ name + ",密码:"+ pwd);
cursor.moveToNext(); // 向下移动一位
}
Toast.makeText(this,"查询成功,查看控制台",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"请输入用户名",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
5 条评论
请问有demo源码学习下吗?
暂无
打卡|´・ω・)ノ
OωO哇,学会增删改查就啥都能干了,加油学习人
四舍五入就是学会了