Android:利用本地服务器-创建数据库-构建用户表(注册)

1.创建一个用户信息类,这里包括你想要记录下的用户信息

public class UserData {

    private String userName;
    private String userPwd;
    private String userPhone;
    private String userMail;
    private String userAds;
    private int userId;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }
    
    public String getUserPhone(){
        return userPhone ;
    }
    
    public void setUserPhone(String userPhone){
        this.userPhone = userPhone;
    }
    
    public String getUserMail(){
        return userMail;
    }
    
    public void setUserMail(String userMail){
        this.userMail = userMail;
    }

    public String getUserAds(){
        return userAds;
    }
    
    public void setUserAds(String userAds){
        this.userAds = userAds;
    }
    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public UserData(int userId,String userName, String userPwd, String userPhone,String userMail,
                    String userAds) {
        super();
        this.userName = userName;
        this.userPwd = userPwd;
        this.userPhone = userPhone;
        this.userMail = userMail;
        this.userAds = userAds;
        this.userId = userId;
    }

    public UserData(String userName, String userPwd,String userPhone,String userMail,
            String userAds) {
        super();
        this.userName = userName;
        this.userPwd = userPwd;
        this.userPhone = userPhone;
        this.userMail = userMail;
        this.userAds = userAds;
    }

}

2.创建用户信息管理类

public class UserDataManager {

    private static final String TAG = "UserDataManager";
    private static final String DB_NAME = "Register.db";
    public static final String TABLE_NAME = "Registry";
    public static final String ID = "_id";
    public static final String USER_NAME = "name";
    public static final String USER_PWD = "pwd";
    public static final String USER_PHONE = "phone";
    public static final String USER_MAIL = "mail";
    public static final String USER_ADD = "address";
    private static final int DB_VERSION = 1;   //版本号
    
    private static final String DB_CREATE = "CREATE TABLE " + TABLE_NAME + " (" +ID + " integer primary key,"+
                                USER_NAME + " varchar,"    + USER_PWD + " varchar,"+ USER_PHONE + " varchar,"+ 
                                USER_MAIL + " varchar," + USER_ADD + " varchar" +  ");";
    
    private Context mContext = null;
    private SQLiteDatabase mSQLiteDatabase = null;
    private DataBaseManagementHelper mDatabaseHelper = null;

    private static class DataBaseManagementHelper extends SQLiteOpenHelper {

        DataBaseManagementHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // 调用时间:数据库第一次创建时onCreate()方法会被调用
            Log.i("调用测试", "OK");
            Log.i(TAG,"db.getVersion()="+db.getVersion());
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
            db.execSQL(DB_CREATE);
            Log.i(TAG, "db.execSQL(DB_CREATE)");
            Log.i(TAG, DB_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // 调用时间:如果DATABASE_VERSION值被改为别的数,系统发现现有数据库版本不同,即会调用onUpgrade
            Log.i(TAG, "DataBaseManagementHelper onUpgrade");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
        
        @Override
        public void onOpen(SQLiteDatabase db)
        {
            super.onOpen(db);
            // 每次打开数据库之后首先被执行
            Log.d(TAG, "DatabaseHelper onOpen");
        }
    }
    
    public UserDataManager(Context context) {
        mContext = context;
        Log.i(TAG, "UserDataManager construction!");
    }

    public void openDataBase() throws SQLException {
        
        mDatabaseHelper = new DataBaseManagementHelper(mContext);
        mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
    }

    public void closeDataBase() throws SQLException {

        mDatabaseHelper.close();
    }

    public void insertUserData(UserData userData) {
        
        String userName=userData.getUserName();
        String userPwd=userData.getUserPwd();
        String userPhone = userData.getUserPhone();
        String userMail = userData.getUserMail();
        String userAds = userData.getUserAds();
    
        mSQLiteDatabase.beginTransaction(); // 开始事务
         try
         {             
             mSQLiteDatabase.execSQL("INSERT INTO " + TABLE_NAME + " VALUES(null, ?, ?, ?, ?, ?)", 
                                 new Object[] { userName,userPwd, userPhone, userMail, userAds });
                 // 带两个参数的execSQL()方法,采用占位符参数?,把参数值放在后面,顺序对应
                 // 一个参数的execSQL()方法中,用户输入特殊字符时需要转义
                 // 使用占位符有效区分了这种情况             
             mSQLiteDatabase.setTransactionSuccessful(); // 设置事务成功完成
         }
         finally
         {
             mSQLiteDatabase.endTransaction(); // 结束事务
         }
    }
    /**
     * 更新整个注册表的用户信息
     * @param userData 用户信息类
     * @return
     */
    public boolean updateUserData(UserData userData) {

        int id = userData.getUserId();
        String userName = userData.getUserName();
        String userPwd = userData.getUserPwd();
        String userPhone = userData.getUserPhone();
        String userMail = userData.getUserMail();
        String userAds = userData.getUserAds();
        
        ContentValues values = new ContentValues();
        values.put(USER_NAME, userName);
        values.put(USER_PWD, userPwd);
        values.put(USER_PHONE, userPhone);
        values.put(USER_MAIL, userMail);
        values.put(USER_ADD, userAds);
        return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
        
    }
    /**
     * query all persons, return cursor
     * 
     * @return Cursor
     */
    public Cursor queryTheCursor()
    {
        Log.d(TAG, "DBManager --> queryTheCursor");
        mSQLiteDatabase = mDatabaseHelper.getReadableDatabase();
        Cursor c = mSQLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return c;
    }
    /**
     * 通过ID号删除用户
     * @param id
     * @return
     */
    public boolean deleteUserData(int id) {

        return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;
    }
    /**
     * 删除所有用户
     * @return
     */
    public boolean deleteAllUserDatas() {

        return mSQLiteDatabase.delete(TABLE_NAME, null, null) > 0;
    }
    /**
     * 通过ID号更新用户表信息
     * @param columnName
     * @param id
     * @param columnValue
     * @return
     */
    public boolean updateUserDataById(String columnName, int id,
            String columnValue) {
        ContentValues values = new ContentValues();
        values.put(columnName, columnValue);
        return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
    }
    /**
     * 通过用户名查找用户
     * @param userName
     * @return
     */
    public int findUserByName(String userName){
        Log.i(TAG,"findUserByName");
        Cursor mCursor = queryTheCursor();
        String name;
        int result=0;
        while(mCursor.moveToNext()){
            Log.i(TAG,"moveToNext");
            name = mCursor.getString(mCursor.getColumnIndex(USER_NAME));
            Log.i(TAG,"name="+name);
            if(name.equals(userName)){
                result = 1;
                Log.i(TAG,"findUserByName , result="+result);
                break;
            }
        }
        return result;
    }
    /**
     * 通过用户名/电话/邮箱和密码查找用户
     * @param userName
     * @param pwd
     * @return
     */
    public int findUserByNameAndPwd(String userName,String pwd){
        Log.i(TAG,"findUserByNameAndPwd()");
        Cursor mCursor = queryTheCursor();
        String name,password,phone,mail;
        int result=0;
        while(mCursor.moveToNext()){
            Log.i(TAG,"moveToNext");
            name = mCursor.getString(mCursor.getColumnIndex(USER_NAME));
            password = mCursor.getString(mCursor.getColumnIndex(USER_PWD));
            phone = mCursor.getString(mCursor.getColumnIndex(USER_PHONE));
            mail = mCursor.getString(mCursor.getColumnIndex(USER_MAIL));
            if((name.equals(userName) || name.equals(mail) || name.equals(phone)) && password.equals(pwd)){
                result = 1;
                Log.i(TAG,"findUserByNameAndPwd , result="+result);
                break;
            }
        }
        return result;
    }
}

3.使用方法

private UserDataManager mUserDataManager;

//首先:创建注册表
    if (mUserDataManager == null) {
        mUserDataManager = new UserDataManager(this);
        mUserDataManager.openDataBase();
    }
    /**
     * 添加新的用户信息时
     * @param Pname    用户名
     * @param Ppwd    密码
     * @param Pphone电话
     * @param Pmail    邮箱
     * @param Plocat地址
     */
     public void addperson(String Pname,String Ppwd, String Pphone, String Pmail, String Plocat){
        UserData mUser = new UserData(Pname,Ppwd,Pphone,Pmail,Plocat);
        mUserDataManager.openDataBase();
        mUserDataManager.insertUserData(mUser);
    }

 

posted @ 2015-07-08 16:00  Bubls  阅读(638)  评论(0编辑  收藏  举报