快速格式化代码:ctrl+alt+l
1. SQList 方式存储数据数据
关键一:监听器过多的情况下如何优化代码。
1.1 添加数据
1. 用db.insert方法
case R.id.add: //获取数据库对象 db = myDbHelper.getWritableDatabase(); String uname = et_uname.getText().toString(); String upwd = et_upwd.getText().toString(); //创建一个ContentValues对象用于存储记录的字段值; // 以键值对存储,键对应字段名,值是字段的具体值 ContentValues contentValues=new ContentValues(); // contentValues.put("userId",1);//因为是自增长所以可以不用写 contentValues.put("userName",uname); contentValues.put("password",upwd); db.insert("az_user",null,contentValues);
2. 用db.execSQL方法
一行搞定
//创建一个ContentValues对象用于存储记录的字段值; // 以键值对存储,键对应字段名,值是字段的具体值 // ContentValues contentValues=new ContentValues(); //// contentValues.put("userId",1);//因为是自增长所以可以不用写 // contentValues.put("userName",uname); // contentValues.put("password",upwd); // db.insert("az_user",null,contentValues); db.execSQL("insert into az_user(userName,password) values(?,?)", new Object[]{uname,upwd});
1.2 查询
1.2.1 查询所有1用query查询
db = myDbHelper.getWritableDatabase(); //Cursor:结果集,结果集中会有游标指向结果集中的某一条记录。 Cursor cursor = db.query("az_user",new String[]{"userName","password"},null,null,null,null,null,null); cursor.moveToFirst(); showInfo.setText("用户名:"+cursor.getString(0)+",密码:"+cursor.getString(1)); while(cursor.moveToNext()){ showInfo.append("\n"+"用户名:"+cursor.getString(0)+",密码:"+cursor.getString(1)); } cursor.close(); db.close();
1.2.1 条件查询1用query查询
case R.id.generate: db=myDbHelper.getWritableDatabase(); String selection = et_selection.getText().toString(); Cursor cursor1 = db.query("az_user",new String[]{"userName","password"},"userName=?",new String[] {selection},null,null,null,null); showInfo.setText("查询结果如下:"); while(cursor1.moveToNext()){ showInfo.append("\n"+"用户名:"+cursor1.getString(0)+"密码:"+cursor1.getString(1)); } cursor1.close(); db.close();
1.2.2 条件查询2用rawQuery查询
case R.id.generate: db=myDbHelper.getWritableDatabase(); String selection = et_selection.getText().toString(); // Cursor cursor1 = db.query("az_user",new String[]{"userName","password"},"userName=?",new String[] {selection},null,null,null,null); Cursor cursor1=db.rawQuery("select userName,password from az_user where userName=?",new String[]{selection}); showInfo.setText("查询结果如下:"); while(cursor1.moveToNext()){ showInfo.append("\n"+"用户名:"+cursor1.getString(0)+"密码:"+cursor1.getString(1)); } cursor1.close(); db.close();
1.3 删除
1.3.1 通过delete方法
db = myDbHelper.getWritableDatabase(); String selection1 = et_selection.getText().toString(); int i=db.delete("az_user","userName=?",new String[]{selection1}); if(i>0){ Toast.makeText(MainActivity.this,"删除成功,删除了"+i+"条",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); } db.close();
1.3.2 通过execSQL方法
注意只能操作,不能返回删掉了几条。
case R.id.delete: db = myDbHelper.getWritableDatabase(); String selection1 = et_selection.getText().toString(); // int i=db.delete("az_user","userName=?",new String[]{selection1}); db.execSQL("delete from az_user where userName=?",new Object[]{selection1}); // if(i>0){ // Toast.makeText(MainActivity.this,"删除成功,删除了"+i+"条",Toast.LENGTH_SHORT).show(); // }else{ // Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); // } db.close(); break;
1.4 修改
用update实现
case R.id.update: db = myDbHelper.getWritableDatabase(); String username1 = et_uname.getText().toString(); String password1 =et_upwd.getText().toString(); String selection2=et_selection.getText().toString(); ContentValues contentValues=new ContentValues(); contentValues.put("userName",username1); contentValues.put("password",password1); db.update("az_user",contentValues,"userName=?",new String[]{selection2}); db.close(); break;
用execSQL实现
case R.id.update: db = myDbHelper.getWritableDatabase(); String username1 = et_uname.getText().toString(); String password1 =et_upwd.getText().toString(); String selection2=et_selection.getText().toString(); db.execSQL("update az_user set userName=?,password=? where userName=?",new Object[]{username1,password1,selection2}); // ContentValues contentValues=new ContentValues(); // contentValues.put("userName",username1); // contentValues.put("password",password1); // db.update("az_user",contentValues,"userName=?",new String[]{selection2}); db.close(); break;
2. 总的实现代码
这里为了方便,条件查询、更新、删除都是通过用户名进行的,也是通过同一个框进行输入。
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" > <ImageView android:layout_width="100dp" android:layout_height="100dp" android:background="@mipmap/ic_launcher" ></ImageView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" ></TextView> <EditText android:id="@+id/uname" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:hint="输入用户名"> </EditText> <EditText android:id="@+id/upwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:textSize="20sp" ></EditText> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="条件查询" android:id="@+id/generate"></Button> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入用户名" android:textSize="20sp" android:id="@+id/selection"></EditText> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="添加"></Button> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="删除"></Button> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="修改"></Button> <Button android:id="@+id/select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:text="查询"></Button> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询结果" android:textSize="25sp" android:id="@+id/showInfo" ></TextView> </LinearLayout>
MainActivity.java
package com.example.myapp; import androidx.annotation.ContentView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.Map; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button generateBtn,addBtn,deleteBtn,updateBtn,selectBtn; private EditText et_uname,et_upwd,et_selection; private TextView showInfo; private MyDbHelper myDbHelper; private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化数据库 myDbHelper=new MyDbHelper(MainActivity.this,"MyDatabase.db",null,666); initView(); //优化,给按钮设置监听器,但是太多按钮了 generateBtn.setOnClickListener(this); addBtn.setOnClickListener(this); deleteBtn.setOnClickListener(this); updateBtn.setOnClickListener(this); selectBtn.setOnClickListener(this); } private void initView() { addBtn = findViewById(R.id.add); generateBtn=findViewById(R.id.generate); deleteBtn = findViewById(R.id.delete); updateBtn = findViewById(R.id.update); selectBtn = findViewById(R.id.select); et_uname = findViewById(R.id.uname); et_upwd = findViewById(R.id.upwd); showInfo = findViewById(R.id.showInfo); et_selection=findViewById(R.id.selection); } @Override public void onClick(View v) { switch (v.getId()){ //1.添加数据 case R.id.add: //获取数据库对象 db = myDbHelper.getWritableDatabase(); String uname = et_uname.getText().toString(); String upwd = et_upwd.getText().toString(); //创建一个ContentValues对象用于存储记录的字段值; // 以键值对存储,键对应字段名,值是字段的具体值 // ContentValues contentValues=new ContentValues(); //// contentValues.put("userId",1);//因为是自增长所以可以不用写 // contentValues.put("userName",uname); // contentValues.put("password",upwd); // db.insert("az_user",null,contentValues); db.execSQL("insert into az_user(userName,password) values(?,?)", new Object[]{uname,upwd}); db.close(); break; //2.查找全部记录 case R.id.select: db = myDbHelper.getWritableDatabase(); //Cursor:结果集,结果集中会有游标指向结果集中的某一条记录。 Cursor cursor = db.query("az_user",new String[]{"userName","password"},null,null,null,null,null,null); cursor.moveToFirst(); showInfo.setText("用户名:"+cursor.getString(0)+",密码:"+cursor.getString(1)); while(cursor.moveToNext()){ showInfo.append("\n"+"用户名:"+cursor.getString(0)+",密码:"+cursor.getString(1)); } cursor.close(); db.close(); break; case R.id.generate: db=myDbHelper.getWritableDatabase(); String selection = et_selection.getText().toString(); // Cursor cursor1 = db.query("az_user",new String[]{"userName","password"},"userName=?",new String[] {selection},null,null,null,null); Cursor cursor1=db.rawQuery("select userName,password from az_user where userName=?",new String[]{selection}); showInfo.setText("查询结果如下:"); while(cursor1.moveToNext()){ showInfo.append("\n"+"用户名:"+cursor1.getString(0)+"密码:"+cursor1.getString(1)); } cursor1.close(); db.close(); //删除 case R.id.delete: db = myDbHelper.getWritableDatabase(); String selection1 = et_selection.getText().toString(); // int i=db.delete("az_user","userName=?",new String[]{selection1}); db.execSQL("delete from az_user where userName=?",new Object[]{selection1}); // if(i>0){ // Toast.makeText(MainActivity.this,"删除成功,删除了"+i+"条",Toast.LENGTH_SHORT).show(); // }else{ // Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); // } db.close(); break; //修改 case R.id.update: db = myDbHelper.getWritableDatabase(); String username1 = et_uname.getText().toString(); String password1 =et_upwd.getText().toString(); String selection2=et_selection.getText().toString(); db.execSQL("update az_user set userName=?,password=? where userName=?",new Object[]{username1,password1,selection2}); // ContentValues contentValues=new ContentValues(); // contentValues.put("userName",username1); // contentValues.put("password",password1); // db.update("az_user",contentValues,"userName=?",new String[]{selection2}); db.close(); break; } } //数据库帮助类 class MyDbHelper extends SQLiteOpenHelper { //构造器作用:参数含义:上下文、数据库文件名称、结果集工厂、数据库版本号 public MyDbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } //数据库初始化创建表或者视图文件 @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table az_user(userId integer primary key autoincrement, userName varchar(20),password varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } }
标签:userName,10,String,改查,APP,db,contentValues,new,null From: https://www.cnblogs.com/hmy22466/p/17225182.html