Android Simple login and registration function realizes +sqlite database learning

Source: Internet
Author: User
Tags sqlite database

Recently beginner Android, did the lab teacher to give the basic task, is simple login and registration, and can be realized through SQLite login, Sqllite is embedded in the Android device

Okay, here's the main code:

Establishment of the database:

Here I just built a simple form for storing usernames and passwords

Mydbhelper.java

 Public classMydbhelperextendsSqliteopenhelper { Public Static FinalString create_userdata= "CREATE table USERDATA (" + "ID integer primary key autoincrement" + "name," + "Password text"); PrivateContext Mcontext;  PublicMydbhelper (context context, String name, Sqlitedatabase.cursorfactory cursorfactory,intversion) {        Super(context,name,cursorfactory,version); Mcontext=context; }     Public  voidonCreate (Sqlitedatabase db) {db.execsql (create_userdata); }     Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {        //onCreate (db);    }}

Registered:

Logon.java

 Public classMain2activityextendsappcompatactivity {PrivateMydbhelper DBHelper; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_main2); DBHelper=NewMydbhelper ( This, "Userstore.db",NULL, 1); }     Public voidlogon (view view) {EditText EditText3=(EditText) Findviewbyid (R.ID.EDITTEXT3); EditText EDITTEXT4=(EditText) Findviewbyid (R.ID.EDITTEXT4); String name=Edittext3.gettext (). toString (); String Password=Edittext4.gettext (). toString (); if(Register (Name,password)) {Toast.maketext ( This, "Insert data table succeeded", Toast.length_short). Show (); }    }     Public BooleanRegister (String username,string password) {sqlitedatabase db=dbhelper.getwritabledatabase (); //The following comments out are the methods of other database insertions        /*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);*/               //This is a simple syntax for database insertionDb.execsql ("INSERT into UserData (Name,password) VALUES (?,?)"); return true; }}

Landing:

Login.java

 Public classMainactivityextendsappcompatactivity {PrivateMydbhelper DBHelper; PrivateEditText username; PrivateEditText UserPassword; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //requestwindowfeature (window.feature_no_title);Setcontentview (R.layout.activity_main); DBHelper=NewMydbhelper ( This, "Userstore.db",NULL, 1); }         //Click the Register button to enter the registration page     Public voidlogonclicked (view view) {Intent Intent=NewIntent (mainactivity. This, Main2activity.class);    StartActivity (Intent); }    //Click the login button     Public voidloginclicked (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, "Landing success (zy,111)", Toast.length_short). Show (); }        Else{toast.maketext (mainactivity). This, "Login Failed", Toast.length_short). Show (); }    }    //Verify Login     Public BooleanLogin (String username,string password) {sqlitedatabase db=dbhelper.getwritabledatabase (); String SQL= "SELECT * from UserData where name=?" and password=? "; Cursor Cursor= Db.rawquery (SQL,Newstring[] {username, password}); if(Cursor.movetofirst ()) {cursor.close (); return true; }        return false; }

All right, just the sauce.

Android Simple login and registration function realizes +sqlite database learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.