The android Sqlite database of wavelet Linux enables users to log on and register easily !, Android sqlite

Source: Internet
Author: User

The android Sqlite database of wavelet Linux enables users to log on and register easily !, Android sqlite

I read a lot of the operating code of Android SQlite data written by others. They are full of clouds! It is not easy to understand at all. I think it is good to write and the android project is also used, so I will save and share it in my blog!

 

One SQLiteHelper class is automatically reload the addition, deletion, modification, and query functions. The other is a self-defined class that uses Context to pass values. I use Fragment. Use Activity to get rid of getActivity!

Implemented the user login registration function!

Http://www.cnblogs.com/xiaobo-Linux/ (wavelet) Zhao archiving QQ463431476

Public class MySqliteHelper extends SQLiteOpenHelper {// custom access sqlite public MySqliteHelper (Context context) {super (context, "userdb. db ", null, 3); // TODO Auto-generated constructor stub} @ Override public void onCreate (SQLiteDatabase db) {// TODO Auto-generated method stub // http://www.cnblogs.com/xiaobo-Linux/ (wavelet) Zhao archiving QQ463431476 db.exe cSQL ("create table users (id int primary key, name text, pwd text) ") ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }}

 

Public class Userlogin extends Activity implements OnClickListener {private MySqliteHelper helper; Button sign; Button reg; String name; String mypwd; private EditText user; private EditText pwd; // http://www.cnblogs.com/xiaobo-Linux/ (wavelet) zhao archive QQ463431476 public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the status bar color getWindow (). addFlags (WindowManager. layoutParams. FLA G_DRAWS_SYSTEM_BAR_BACKGROUNDS); getWindow (). setStatusBarColor (getResources (). getColor (R. color. statusBar); // sets the actionbar color: ActionBar actionBar = getActionBar (); actionbar. setBackgroundDrawable (new ColorDrawable (Color. parseColor ("# 0099CC"); setContentView (R. layout. login); findViewById (R. id. button1 ). setOnClickListener (this); findViewById (R. id. button2 ). setOnClickListener (this); user = (EditText) fi NdViewById (R. id. editText1); pwd = (EditText) findViewById (R. id. editText2);} public void insert () {helper = new MySqliteHelper (getApplicationContext (); SQLiteDatabase db = helper. getWritableDatabase (); // check whether the user name is repeated String sql1 = "select * from users"; Cursor cursor = db. rawQuery (sql1, null); while (cursor. moveToNext () {// The first column is id name = cursor. getString (1); // get the value of Column 2nd. The index of the first column starts from 0. mypwd = cursor. getSt Ring (2); // get the value of column 3rd} if (user. getText (). toString (). equals (name) {Toast. makeText (this, "this user already exists, please register again", Toast. LENGTH_SHORT ). show ();} else {String sql2 = "insert into users (name, pwd) values ('" + user. getText (). toString () + "','" + pwd. getText (). toString () + "')"; db.exe cSQL (sql2); Toast. makeText (this, "registration successful! ", Toast. LENGTH_SHORT ). show () ;}} public void select () {helper = new MySqliteHelper (getApplicationContext (); SQLiteDatabase db = helper. getWritableDatabase (); String SQL = "select * from users"; Cursor cursor = db. rawQuery (SQL, null); while (cursor. moveToNext () {// The first column is id name = cursor. getString (1); // get the value of Column 2nd. The index of the first column starts from 0. mypwd = cursor. getString (2); // obtain the value of column 3rd} if (user. getText (). toString (). equals (name) & (pwd. getText (). toString (). equals (mypwd) {Toast. makeText (this, "user verification successful", Toast. LENGTH_SHORT ). show (); Intent MainActivity = new Intent (); MainActivity. setClass (this, MainActivity. class); this. startActivity (MainActivity); finish (); // exit} else {Toast. makeText (this, "the account or password is incorrect. Please try again", Toast. LENGTH_SHORT ). show ();} cursor. close (); db. close (); // Toast. makeText (this, "database closed", Toast. LENGTH_SHORT ). show () ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stub switch (v. getId () {case R. id. button1: select (); break; case R. id. button2: insert (); break ;}}

Http://www.cnblogs.com/xiaobo-Linux/ (wavelet) Zhao archiving QQ463431476

The effect is as follows:

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.