android简单登陆和注册功能实现+SQLite数据库学习
发布时间:-07-04 17:23,
浏览次数:1027
, 标签:
android
SQLite
这里我只是建立了一个用简单的存储用户名和密码的表单
MyDBHelper.java
<>public class MyDBHelper extends SQLiteOpenHelper { public static final
String CREATE_USERDATA="create table userData(" + "id integer primary key
autoincrement," +"nametext," +"password text)"; private Context mContext; public
MyDBHelper(Context context, String name, SQLiteDatabase.CursorFactory
cursorFactory,int version){ super(context,name,cursorFactory,version); mContext=
context; }public void onCreate(SQLiteDatabase db){ db.execSQL(CREATE_USERDATA);
}public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ //
onCreate(db); } } <>
注册:
Main2Activity.java <>public class Main2Activity extends AppCompatActivity {
private MyDBHelper dbHelper; @Override protected void onCreate(Bundle
savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); dbHelper = new
MyDBHelper(this,"UserStore.db",null,1); } public void logon(View view){
//SQLiteDatabase db=dbHelper.getWritableDatabase(); EditText
editText3=(EditText)findViewById(R.id.editText3); EditText
editText4=(EditText)findViewById(R.id.editText4); String newname
=editText3.getText().toString(); String
password=editText4.getText().toString(); if
(CheckIsDataAlreadyInDBorNot(newname)) {
Toast.makeText(this,"该用户名已被注册,注册失败",Toast.LENGTH_SHORT).show(); } else { if
(register(newname, password)) { Toast.makeText(this, "插入数据表成功",
Toast.LENGTH_SHORT).show(); } } } //向数据库插入数据 public boolean register(String
username,String password){ SQLiteDatabase db= dbHelper.getWritableDatabase();
/*String sql = "insert into userData(name,password) value(?,?)"; Object
obj[]={username,password}; db.execSQL(sql,obj);*/ ContentValues values=new
ContentValues(); values.put("name",username); values.put("password",password);
db.insert("userData",null,values); db.close(); //db.execSQL("insert into
userData (name,password) values (?,?)",new String[]{username,password}); return
true; } //检验用户名是否已存在 public boolean CheckIsDataAlreadyInDBorNot(String value){
SQLiteDatabase db=dbHelper.getWritableDatabase(); String Query = "Select * from
userData where name =?"; Cursor cursor = db.rawQuery(Query,new String[] { value
}); if (cursor.getCount()>0){ cursor.close(); return true; } cursor.close();
return false; } } <>
登陆:
MainActivity.java <>public class MainActivity extends AppCompatActivity {
private MyDBHelper dbHelper; private EditText username; private EditText
userpassword; @Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); dbHelper= new MyDBHelper(this
,"UserStore.db",null,1); } //点击注册按钮进入注册页面 public void logonClicked(View view){
Intent intent= new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent); }//点击登录按钮 public void loginClicked(View view) { username=
(EditText)findViewById(R.id.editText2); userpassword=
(EditText)findViewById(R.id.editText); String userName=
username.getText().toString(); String passWord=
userpassword.getText().toString();if (login(userName,passWord)) {
Toast.makeText(MainActivity.this, "登陆成功(ZY,111)", Toast.LENGTH_SHORT).show(); }
else { Toast.makeText(MainActivity.this, "登陆失败", Toast.LENGTH_SHORT).show(); } }
//验证登录 public boolean login(String username,String password) { SQLiteDatabase db
= dbHelper.getWritableDatabase(); String sql = "select * from userData where
name=? and password=?"; Cursor cursor = db.rawQuery(sql, new String[]
{username, password});if (cursor.moveToFirst()) { cursor.close(); return true; }
return false; } <>
如果觉得《android mysql实现登录注册_android简单登陆和注册功能实现+SQLite数据库学习》对你有帮助,请点赞、收藏,并留下你的观点哦!