Android SqlLite的简单实用

1.先写一个MySqlHelper类,用于创建数据库

 

public class MySqlHelper extends SQLiteOpenHelper{
    public MySqlHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table if not exists user(_id Integer primary key autoincrement ,uname varchar(20),upass varchar(20))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2.写个Dialog的页面布局zhuce_item.xml

 

 




    
    

3.LoginActivity页面的代码:

 

 

public class LoginActivity extends AppCompatActivity {

    private EditText editName;
    private EditText editPass;
    private Button login;
    private Button zhuce;
    private Button update;
    private MySqlHelper helper;
    private SQLiteDatabase read;
    private SQLiteDatabase write;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();

        initData();
    }
    /*获取控件,创建数据库对象*/
    private void initView() {
        editName = (EditText) findViewById(R.id.name);
        editPass = (EditText) findViewById(R.id.pass);
        login = (Button) findViewById(R.id.login);
        zhuce = (Button) findViewById(R.id.zhuce);
        update = (Button) findViewById(R.id.update);
        //数据库helper对象
        helper = new MySqlHelper(LoginActivity.this, "user", null, 2);
        //读取数据库数据的对象
        read = helper.getReadableDatabase();
        //写入数据库数据的对象
        write = helper.getWritableDatabase();
    }
    private void initData() {
        //登陆点击事件
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(LoginActivity.this,"登录",Toast.LENGTH_SHORT).show();
                String name = editName.getText().toString();
                String pass = editPass.getText().toString();
                //查询语句
                String sql = "select * from user where uname=? and upass=?";
                Cursor query = read.rawQuery(sql,new String[]{name,pass});
                if(query.moveToNext()==true){
                    Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(LoginActivity.this,"账号或密码错误,请注册",Toast.LENGTH_SHORT).show();
                }

            }
        });

        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(LoginActivity.this,"注册",Toast.LENGTH_SHORT).show();

                AlertDialog.Builder dialog = new AlertDialog.Builder(LoginActivity.this);
                final View view1 = LayoutInflater.from(LoginActivity.this).inflate(R.layout.zhuce_item,null);

                dialog.setView(view1);
                dialog.setTitle("注册页面")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText newname = (EditText) view1.findViewById(R.id.zhucename);
                        EditText newpass = (EditText) view1.findViewById(R.id.zhucepass);
                        String name = newname.getText().toString();
                        String pass = newpass.getText().toString();
                        //添加语句
                        String sql = "insert into user(uname,upass) values(?,?)";
                        write.execSQL(sql,new String[]{name,pass});
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .show();
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(LoginActivity.this,"忘记密码",Toast.LENGTH_SHORT).show();
                AlertDialog.Builder dialog = new AlertDialog.Builder(LoginActivity.this);
                //引入Dialog的布局
                final View view1 = LayoutInflater.from(LoginActivity.this).inflate(R.layout.zhuce_item,null);
                dialog.setView(view1);
                dialog.setTitle("修改密码")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText newname = (EditText) view1.findViewById(R.id.zhucename);
                        EditText newpass = (EditText) view1.findViewById(R.id.zhucepass);
                        String name = newname.getText().toString();
                        String pass = newpass.getText().toString();
                        //修改语句
                        String sql1 = "update user set uname=?,upass=?where uname=?";
                        write.execSQL(sql1,new String[]{name,pass,name});
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })
                .show();
            }
        });
    }
}






 

你可能感兴趣的:(Android)