星空网 > 软件开发 > 操作系统

小波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

效果如下:

小波Linux安卓Sqlite数据库实现用户登录注册,通俗易懂!images/loading.gif' data-original="http://images2015.cnblogs.com/blog/782772/201605/782772-20160529191136116-75001694.jpg" />




原标题:小波Linux安卓Sqlite数据库实现用户登录注册,通俗易懂!

关键词:linux

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

海运 海运 美国:https://www.goluckyvip.com/tag/99181.html
天津到南非海运:https://www.goluckyvip.com/tag/99182.html
广州海运南非:https://www.goluckyvip.com/tag/99183.html
上海越南海运:https://www.goluckyvip.com/tag/99184.html
海运去南非:https://www.goluckyvip.com/tag/99185.html
苏里南海运:https://www.goluckyvip.com/tag/99186.html
无锡旅游景点竹海 - 无锡的竹海:https://www.vstour.cn/a/363178.html
5月贾汪好玩的地方 贾汪哪有好玩的地方:https://www.vstour.cn/a/363179.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流