帐号管理的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(); }
相关推荐
specialbrian 2020-07-31
loveandroid0 2020-06-18
DAV数据库 2020-06-17
URML 2020-06-13
Dlanguage 2020-06-12
Plant 2020-06-07
疯狂老司机 2020-06-07
chibangyuxun 2020-06-07
sdwylry 2020-06-04
airfling 2020-05-31
Plant 2020-05-31
zbcaicai 2020-05-26
chaochao 2020-05-19
Plant 2020-05-17
小火车 2020-05-14
beibeijia 2020-04-25
Rain 2020-04-16