Android-数据库操作

public class SqlDao {

private MyDBOpenHelper dbOpenHelper;

public SqlDao(Context context) {
  dbOpenHelper = new MyDBOpenHelper(context);
}

// 增加
public void addUser(String username, String password) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  if (db.isOpen()) {
   ContentValues values  =  new ContentValues();
   values.put("username", username);
   values.put("password", password);
   db.insert("user", null, values);
   /*db.execSQL("insert into user(username,password) values(?,?)",
     new Object[] { username, password });*/
  }
  db.close();
}

// 删除
public void deleteUser(String username) {
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  if(db.isOpen()){
   db.delete("user", "username=?", new String[]{username});
   //db.execSQL("delete from user where username=?", new Object[]{username});
  }
  db.close();
}

// 查找
public User findUser(String username) {
  User user = null;
  SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
  /*if (db.isOpen()) {
   Cursor cursor = db.rawQuery("select * from user where username=?",
     new String[] { username });
   if (cursor.moveToFirst()) {
    user=new User();
    int index  = cursor.getColumnIndex("username");
    String name= cursor.getString(index);
    user.setUsername(name);
    int index2  = cursor.getColumnIndex("password");
    String pwd= cursor.getString(index2);
    user.setPassword(pwd);
    cursor.close();
   }
  }*/
  Cursor cursor = db.query("user", null, "username=?", new String[]{username}, null, null, null);
  if(cursor.moveToFirst()){
   while(cursor.moveToNext()){
    user = new User();
    int columnIndexUsername = cursor.getColumnIndex("username");
    int columnIndexPassword = cursor.getColumnIndex("password");
    String name=  cursor.getString(columnIndexUsername);
    String pwd= cursor.getString(columnIndexPassword);
    user.setUsername(name);
    user.setPassword(pwd);
   }
  }
  db.close();
  return user;
}

// 修改
public void updateUser(String oldName, String newName, String newPassword) {
  User user = findUser(oldName);
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  if(user!=null){
   //db.execSQL("update user set username=?,password=? where username=?", new Object[]{newName,newPassword,oldName});
   ContentValues values = new ContentValues();
   values.put("username", newName);
   values.put("password", newPassword);
   db.update("user", values, "username=?", new String[]{oldName});
  }
  db.close();
}
public List getAll(){
  List list = new ArrayList();
  User user = null;
  SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
  if(db.isOpen()){
   Cursor cursor = db.query("user", null, null, null, null, null, null);
   if(cursor.moveToFirst()){
    while(cursor.moveToNext()){
     user = new User();
     int columnIndexUsername = cursor.getColumnIndex("username");
     int columnIndexPassword = cursor.getColumnIndex("password");
     String name=  cursor.getString(columnIndexUsername);
     String pwd= cursor.getString(columnIndexPassword);
     user.setUsername(name);
     user.setPassword(pwd);
     list.add(user);
    }
   }
   cursor.close();
  }
  db.close();
  return list;
}
}

--------------------------------------------------------------------------------------------------------------------------------

public class MyDBOpenHelper extends SQLiteOpenHelper {
/**
  *
  * @param context 应用程序上下文
  * @param name    数据库的名字
  * @param factory 查询数据库的游标工厂 一般情况下 用sdk默认的
  * @param version  数据库的版本 版本号必须不小1
  * 
  */
public MyDBOpenHelper(Context context) {
  super(context, "mydb.db", null , 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
  System.out.println("oncreat被调用");
  db.execSQL("CREATE TABLE user (userid integer primary key autoincrement , username varchar(40),password varchar(40))");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  System.out.println("update被调用");
  db.execSQL("ALTER TABLE person ADD phone VARCHAR(12) NULL ");
}

}

你可能感兴趣的:(Android)