The wavelet linux Android SQLite database realizes user login registration, easy to understand!

Source: Internet
Author: User
Tags sqlite database

Read a lot of other people wrote the Android SQLite data operation code, are floating clouds, blind! It is not easy to understand, I think I write well, and the Android project is also used, so in the blog park to save and share a bit!

A Sqlitehelper class is an automatic overload of adding and deleting functions, and the other is a class of its own definition, using the context to pass values. I use fragment, with activity words getactivity () Remove!

Realize the user login registration Function!

http://www.cnblogs.com/xiaobo-Linux/(wavelet) Zhao Archive QQ463431476

 Public classMysqlitehelperextendsSqliteopenhelper {//Custom Access SQLite     PublicMysqlitehelper (Context context) {Super(Context, "userdb.db",NULL, 3); //TODO auto-generated Constructor stub} @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method Stub//http://www.cnblogs.com/xiaobo-linux/(wavelet) Zhao Archive QQ463431476 db.execsql ("CREATE table users (ID int primary key, name text,pwd text)"); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {        //TODO auto-generated Method Stub            } }

 Public classUserloginextendsActivityImplementsonclicklistener{PrivateMysqlitehelper Helper;         Button sign;              Button reg;       String name;       String MyPwd; PrivateEditText User; PrivateEditText pwd; http://www.cnblogs.com/xiaobo-Linux/(wavelet) Zhao Archive QQ463431476 Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set the status bar colorGetWindow (). Addflags (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);          GetWindow (). Setstatusbarcolor (Getresources (). GetColor (R.color.statusbar)); //Set Actionbar colorActionBar ActionBar =Getactionbar (); Actionbar.setbackgrounddrawable (NewColordrawable (Color.parsecolor ("#0099CC")));              Setcontentview (R.layout.login); Findviewbyid (R.id.button1). Setonclicklistener ( This); Findviewbyid (R.id.button2). Setonclicklistener ( This); User=(EditText) Findviewbyid (R.ID.EDITTEXT1); PWD=(EditText) Findviewbyid (R.ID.EDITTEXT2); }        Public voidInsert () {helper=NewMysqlitehelper (Getapplicationcontext ()); Sqlitedatabase DB=helper.getwritabledatabase (); //Check If the user name is duplicatedString SQL1 = "SELECT * from Users"; Cursor Cursor= Db.rawquery (SQL1,NULL);  while(Cursor.movetonext ()) {//first column IDName = cursor.getstring (1);//gets the value of the 2nd column, starting with the index of the first column 0MyPwd = cursor.getstring (2);//gets the value of the 3rd column               }           if((User.gettext (). toString (). Equals (name)) {Toast.maketext ( This, "This user already exists, please re-register", Toast.length_short). Show (); }         Else{String sql2= "INSERT into users (NAME,PWD) VALUES ('" +user.gettext (). ToString () + "', '" +pwd.gettext (). ToString () + "')";               Db.execsql (SQL2); Toast.maketext ( This, "Registered successfully! ", Toast.length_short). Show (); }      }    Public voidSelect () {helper=NewMysqlitehelper (Getapplicationcontext ()); Sqlitedatabase DB=helper.getwritabledatabase (); String SQL= "SELECT * from Users"; Cursor Cursor= Db.rawquery (SQL,NULL);  while(Cursor.movetonext ()) {//first column IDName = cursor.getstring (1);//gets the value of the 2nd column, starting with the index of the first column 0MyPwd = cursor.getstring (2);//gets the value of the 3rd column               }            if((User.gettext (). toString (). Equals (name)) &&(Pwd.gettext (). toString (). Equals (MyPwd))) {Toast.maketext ( This, "User authentication succeeded", Toast.length_short). Show (); Intent mainactivity=NewIntent (); Mainactivity. SetClass ( This, Mainactivity.class);  This. StartActivity (mainactivity); Finish ();//Exit         }         Else{Toast.maketext ( This, "The account or password is wrong, please re-enter", Toast.length_short). Show ();                } cursor.close ();                Db.close (); //Toast.maketext (This, "database has been closed", Toast.length_short). Show (); } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub       Switch(V.getid ()) { CaseR.id.button1:select ();  Break;  CaseR.id.button2:insert ();  Break; } }     

http://www.cnblogs.com/xiaobo-Linux/(wavelet) Zhao Archive QQ463431476

The effect is as follows:

The wavelet linux Android SQLite database realizes user login registration, easy to understand!

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.