今天学习了连接数据库查询功能实现登录功能
package com.example.helloworld; import static android.widget.Toast.LENGTH_SHORT; import static com.example.helloworld.R.*; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; 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.Toast; public class MainActivity extends AppCompatActivity { Button denglu = null; Button zhuce = null; String uuser, ppwd; EditText user, pwd; SQLiteDatabase db; int x; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(layout.activity_main); user = findViewById(id.user); pwd = findViewById(id.pwd); denglu=findViewById(id.denglu); zhuce=findViewById(id.zhuce); } public void Denglu(View view) { x=0; uuser=user.getText().toString(); ppwd=pwd.getText().toString(); MyDbhelper myDbhelper = new MyDbhelper(MainActivity.this, "Data.db", null, 666); db = myDbhelper.getWritableDatabase(); Cursor cursor=db.query("user",new String[]{"Id","pwd"},null,null,null,null,null,null); cursor.moveToFirst(); if(uuser.equals(cursor.getString(0))==true&&ppwd.equals(cursor.getString(1))==true){ db.close(); Toast.makeText(MainActivity.this, "登录成功",LENGTH_SHORT ).show(); Intent intent = new Intent(MainActivity.this, ZhuyemianActivity.class); this.startActivity(intent); x=1; } while(cursor.moveToNext()){ if(uuser.equals(cursor.getString(0))==true&&ppwd.equals(cursor.getString(1))==true){ db.close(); Toast.makeText(MainActivity.this, "登录成功",LENGTH_SHORT ).show(); Intent intent = new Intent(MainActivity.this, ZhuyemianActivity.class); this.startActivity(intent); x=1; } } if(x==0) { AlertDialog alertDialog1 = new AlertDialog.Builder(this) .setTitle("提示")//标题 .setMessage("密码错误")//内容 .create(); alertDialog1.show(); db.close(); } } public void Zhuce(View view) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); this.startActivity(intent); } 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,Id varchar(20),pwd varchar(20),name varchar(20),banji varchar(20),phone varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } }
标签:总结,3.08,db,MainActivity,Intent,import,今日,android,null From: https://www.cnblogs.com/yangkaiwen/p/17196437.html