用户数据库建立

数据库的建立

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 query(){
    Cursor cursor=db.query("user_info",null,null,null,null,null,null);
    List lists=new ArrayList();
    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();
}
}

你可能感兴趣的:(用户数据库建立)