日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

android mysql实现登录注册_android简单登陆和注册功能实现+SQLite数据库学习

發布時間:2025/3/8 数据库 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android mysql实现登录注册_android简单登陆和注册功能实现+SQLite数据库学习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android簡單登陸和注冊功能實現+SQLite數據庫學習

發布時間:2018-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数据库学习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。