你的位置:首页 > 操作系统

[操作系统]小波Linux安卓Sqlite数据库实现用户登录注册,通俗易懂!


看了很多别人写的安卓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

效果如下: