今日总结-数据库的链接
主要用了一些sql语句和学习了基本的增删改查的语句,完成了数据库的链接。
代码如下:
mainactivity代码:
package com.example.sqlite; 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; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button generateBtn, addBtn, deleteBtn, updateBtn, selectBtn; private EditText et_username, et_password, 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(); addBtn.setOnClickListener(this); selectBtn.setOnClickListener(this); generateBtn.setOnClickListener(this); deleteBtn.setOnClickListener(this); updateBtn.setOnClickListener(this); } private void initView() { generateBtn = findViewById(R.id.generate); addBtn = findViewById(R.id.add); deleteBtn = findViewById(R.id.delete); updateBtn = findViewById(R.id.update); selectBtn = findViewById(R.id.select); et_username = findViewById(R.id.username); et_password = findViewById(R.id.password); et_selection = findViewById(R.id.selection); showInfo = findViewById(R.id.showInfo); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add: db = myDbHelper.getWritableDatabase(); String username = et_username.getText().toString(); String password = et_password.getText().toString(); //创建一个ContentValues对象,用于存储记录的字段值,以键值对的方式存储,“键“对应就是字段名,”值"对应的就是某个字段的具体的值 // ContentValues contentValues=new ContentValues(); //contentValues.put("username",username); //contentValues.put("password",password); //db.insert("user",null,contentValues); db.execSQL("insert into user(username,password) values(?,?)", new Object[]{username, password}); //?占位符 db.close(); break; case R.id.select: showInfo.setText(" "); db = myDbHelper.getWritableDatabase(); //Cursor:结果集,结果集中会有游标,游标会指向结果集中的某一条记录,我们获取的记录是哪一条,初始时指向第一条记录 Cursor cursor = db.query("user", new String[]{"username", "password"}, null, null, null, null, null, null); 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("user", new String[]{"username", "password"}, "username=?", new String[]{selection}, null, null, null, null); Cursor cursor1 = db.rawQuery("select username,password from user where username=?", new String[]{selection}); showInfo.setText("查询结果如下:"); while (cursor1.moveToNext()) { showInfo.append("\n" + "用户名:" + cursor1.getString(0) + ",密码:" + cursor1.getString(1)); } cursor1.close(); db.close(); break; case R.id.delete: db = myDbHelper.getWritableDatabase(); String selection1 = et_selection.getText().toString(); int i = db.delete("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(); break; case R.id.update: db = myDbHelper.getWritableDatabase(); String username1 = et_username.getText().toString(); String password1= et_password.getText().toString(); String selection2 = et_selection.getText().toString(); //创建一个ContentValues对象,用于存储记录的字段值,以键值对的方式存储,“键“对应就是字段名,”值"对应的就是某个字段的具体的值 ContentValues contentValues=new ContentValues(); contentValues.put("username",username1); contentValues.put("password",password1); db.update("user",contentValues,"username=?",new String[]{selection2}); db.close(); break; } } //快速格式化的快捷键ctrl+alt+l //数据库帮助类 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 user(user_id integer primary key autoincrement,userName varchar(10),password varchar(10))"); } //升级方法 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } }
布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <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" tools:context=".MainActivity" android:orientation="vertical"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:src="@mipmap/ic_launcher" ></ImageView> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textColor="@color/black"></TextView> <EditText android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入用户名" android:textColorHint="@color/black"></EditText> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textColor="@color/black"></TextView> <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入密码" android:password="true" android:textColorHint="@color/black"></EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="条件查询" android:id="@+id/generate" ></Button> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入查询用户名:" android:layout_gravity="center" android:id="@+id/selection"></EditText> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add" android:onClick="dengluclick" android:text="添加" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/delete" android:onClick="zhuceclick" android:text="删除" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="zhuceclick" android:id="@+id/update" android:text="修改" ></Button> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="zhuceclick" android:id="@+id/select" android:text="查询" ></Button> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示查询结果" android:gravity="center" android:layout_gravity="center" android:id="@+id/showInfo" android:textColor="@color/black"></TextView> </LinearLayout>