帐号管理的SQLite

package com.kongmw.common;

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

/**
 * 帐号管理的SQLite
 * @author Administrator
 *
 */
public class AccountDB extends SQLiteOpenHelper {
	
	private final static String DATADB_NAME = "ACCOUNT.db";
	private String TABLE_NAME = "account_table";
	private String ACCOUNT_ID = "account_id";
	private String ACCOUNT_USERNAME = "account_username";
	private String ACCOUNT_USERPWD = "account_userpwd";	//如果是第三方的方式登录,sinaweibo登录存入的是userid
	private String ACCOUNT_LGOINLY = "account_loginly";	//登录的方式
	private String ACCOUNT_FACE = "account_face"; //账号头像

	public AccountDB(Context context) {
		super(context, DATADB_NAME, null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "CREATE TABLE " + TABLE_NAME + " (" + ACCOUNT_ID
				+ " INTEGER primary key autoincrement, " + ACCOUNT_USERNAME
				+ " text, " + ACCOUNT_USERPWD + " text, " + ACCOUNT_LGOINLY
				+ " text, " + ACCOUNT_FACE + " text);";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub
		String sql = "DROP TABLE IF EXISTS "+TABLE_NAME;
		db.execSQL(sql);
		onCreate(db);
	}
	
	//删除所有数据
	public void deleteDB() {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(TABLE_NAME, "1", null);
	}
	
	//查询操作
	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db
				.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}
	
	//添加操作
	public void insert(String username, String pwd, String loginly, String faceurl) {
		boolean isCanadd = true;
		//添加之前先判断是否存在重复
		Cursor aCursor = select();
		aCursor.moveToFirst();
		while(!aCursor.isAfterLast()) {
			String name = aCursor.getString(1);
			String userpw = aCursor.getString(2);
			if(username.equals(name) && pwd.equals(userpw)) {	//存在重复
				isCanadd = false;
				break;
			}
			aCursor.moveToNext();
		}
		if(!aCursor.isClosed()) {
			aCursor.close();
		}
		if(isCanadd) {
			SQLiteDatabase db = this.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put(ACCOUNT_USERNAME, username);
			cv.put(ACCOUNT_USERPWD, pwd);
			cv.put(ACCOUNT_LGOINLY, loginly);
			cv.put(ACCOUNT_FACE, faceurl);
			db.insert(TABLE_NAME, null, cv);
		}
	}
	
	//删除操作
	public void delete(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = ACCOUNT_ID+" = ?";
		String[] whereValue = {Integer.toString(id)};
		db.delete(TABLE_NAME, where, whereValue);
	}

}

在登录的时候,添加账户到数据库

try {
					AccountDB accountDB = new AccountDB(LoginActivity.this);
					accountDB.insert(userBean.getUsername(), upwd, loginly,
							userBean.getFaceurl());
					accountDB.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

  

相关推荐