小波Linux安卓Sqlite資料庫實現使用者登入註冊,通俗易懂!

來源:互聯網
上載者:User

標籤:

看了很多別人寫的安卓SQlite資料的作業碼,都是浮雲,瞎弄!一點也不通俗易懂,我覺得我寫的不錯,而且安卓項目也用上了,所以在部落格園裡儲存分享一下!

 

一SQLiteHelper類是自動重載增刪改查函數的,另外一個是自己定義的類,用Context傳值。我用的是Fragment,用Activity的話吧getActivity()去掉!

實現了使用者登入註冊的功能!

http://www.cnblogs.com/xiaobo-Linux/ (小波)趙存檔QQ463431476

public class MySqliteHelper extends SQLiteOpenHelper {    //自訂訪問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/ (小波)趙存檔QQ463431476        db.execSQL("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/ (小波)趙存檔QQ463431476    public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);       //設定狀態列顏色        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);       getWindow().setStatusBarColor(getResources().getColor(R.color.StatusBar));          //設定actionbar顏色        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)findViewById(R.id.editText1);       pwd=(EditText)findViewById(R.id.editText2);        }       public void  insert() {           helper = new MySqliteHelper(getApplicationContext());      SQLiteDatabase db=helper.getWritableDatabase();                //查詢一下,是否使用者名稱重複       String sql1 = "select * from users";       Cursor cursor = db.rawQuery(sql1, null);       while (cursor.moveToNext()) {          //第一列為id          name =  cursor.getString(1); //擷取第2列的值,第一列的索引從0開始          mypwd = cursor.getString(2);//擷取第3列的值               }           if((user.getText().toString().equals(name)))         {                Toast.makeText(this, "已存在此使用者,請重新註冊", 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, "註冊成功!", 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()) {          //第一列為id          name =  cursor.getString(1); //擷取第2列的值,第一列的索引從0開始          mypwd = cursor.getString(2);//擷取第3列的值               }            if((user.getText().toString().equals(name))&&(pwd.getText().toString().equals(mypwd)))         {                Toast.makeText(this, "使用者驗證成功", Toast.LENGTH_SHORT).show();                 Intent MainActivity = new Intent();                MainActivity .setClass(this,MainActivity.class);                  this.startActivity(MainActivity);                    finish();//退出         }         else          {                Toast.makeText(this, "帳號或者密碼錯誤,請重新輸入", Toast.LENGTH_SHORT).show();            }                      cursor.close();                db.close();                //Toast.makeText(this, "已經關閉資料庫", 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/ (小波)趙存檔QQ463431476

效果如下:

小波Linux安卓Sqlite資料庫實現使用者登入註冊,通俗易懂!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.