结队项目-数据库的建立

数据库的建立

package com.example.myapplication3.db;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;

    import com.example.myapplication3.model.UserInfo;

    import java.util.ArrayList;
    import java.util.List;

public class DBManager {
    private DBHelper helper;
    private SQLiteDatabase db;

public DBManager(Context context) {
    helper=new DBHelper(context);
    db=helper.getWritableDatabase();
}

//注册用户
public void insert(UserInfo info){
    ContentValues values=new ContentValues();
    values.put("name",info.getName());
    values.put("pwd",info.getPwd());
    db.insert("user_info",null,values);
}

//查询全部用户名
public List<UserInfo> query(){
    Cursor cursor=db.query("user_info",null,null,null,null,null,null);
    List<UserInfo> lists=new ArrayList<UserInfo>();
    if (cursor.getCount()!=0){
        cursor.moveToFirst();
        do {
            UserInfo info=new UserInfo(cursor.getString(1),cursor.getString(2));
            lists.add(info);
        }while (cursor.moveToNext());
        return lists;
    }
    return lists;
}

//判断数据库中此用户是否已存在
public int find(String name){
    Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
    if (cursor.getCount()!=0){
        return 1;
    }
    return 0;
}

//查询单个用户
public UserInfo findUser(String name){
    Cursor cursor=db.query("user_info",null,"name=?",new String[]{name},null,null,null);
    if (cursor.getCount()!=0){
        cursor.moveToFirst();
        UserInfo info=new UserInfo();
        info.setName(cursor.getString(cursor.getColumnIndex("name")));
        info.setPwd(cursor.getString(cursor.getColumnIndex("pwd")));
        return info;
    }
    return null;
}

public void closeDB(){
    if (db!=null){
        db.close();
    }
}

}

注册功能实现

package com.example.myapplication3.activity;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.example.myapplication3.R;
    import com.example.myapplication3.db.DBManager;
    import com.example.myapplication3.model.UserInfo;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

private EditText et_register_name;
private EditText et_register_pwd1;
private EditText et_register_pwd2;
private Button btn_register_register;
private DBManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    initView();
    manager = new DBManager(RegisterActivity.this);
}

private void initView() {
    et_register_name = (EditText) findViewById(R.id.register_name);
    et_register_pwd1 = (EditText) findViewById(R.id.register_pwd1);
    et_register_pwd2 = (EditText) findViewById(R.id.register_pwd2);
    btn_register_register = (Button) findViewById(R.id.btn_register_register);

    btn_register_register.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_register_register:
            submit();
            break;
    }
}

private void submit() {
    // validate
    String name = et_register_name.getText().toString().trim();
    String pwd1 = et_register_pwd1.getText().toString().trim();
    String pwd2 = et_register_pwd2.getText().toString().trim();
    if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)) {
        Toast.makeText(this, "注册用户不能为空", Toast.LENGTH_SHORT).show();
        return;
    }
    // TODO validate success, do something
    if (TextUtils.equals(pwd1, pwd2)) {
        if (manager.find(name) == 0) {
            UserInfo info = new UserInfo(name, pwd1);
            manager.insert(info);
            finish();
        } else {
            Toast.makeText(this, "此用户已存在,请重新注册", Toast.LENGTH_SHORT).show();
            return;
        }
    } else {
        Toast.makeText(this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
        return;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    manager.closeDB();
}

}

转载于:https://www.cnblogs.com/szj1996/p/6986493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值