android sqlite操作

一:
    onCreate 方法,在dbHelper.getWriteable() 就会执行onCreate()方法 可以将创建表的sql语句写在里面
二:
异常:
 android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x220f28 at
处理方法
  Cursor result=db.query("widgets", columns, "name=?",parms, null, null, null);    一定要加上 =? 才行
 
三:应用
 Cursor c=db.rawQuery( "SELECT name FROM sqlite_master WHERE type='table' AND name='mytable'", null);
 Cursor c = db.rawQuery("SELECT * FROM table WHERE android123=?", args)
rawQuery使用sql语句,query构建一个sql语句 尽量用前者

 Cursor result=db.rawQuery("SELECT ID, name, inventory FROM mytable"); 
    result.moveToFirst();
    while (!result.isAfterLast()) {
        int id=result.getInt(0);
        String name=result.getString(1); //getString getInt 得到字段当前记录的值
        int inventory=result.getInt(2);
        // do something useful with these
        result.moveToNext();   
      }
      result.close();    //释放游标资源
     
四:更改操作:
   
    db.execSQL("INSERT INTO widgets (name, inventory)"+ "VALUES ('Sprocket', 5)");
   
    db.delete("user", "id =?", new String[]{"1"});
    String where=FIELD_ID+"=?";
    String[] whereValue={Integer.toString(id)};
    db.delete(TABLE_NAME, where, whereValue);
   
    /*update*/
    values = new ContentValues();
    values.put("name","tom")
    db.update("user", values, " id=?", new String[]{"1"});
     
    SQLiteDatabase db=this.getWritableDatabase();
        String where=FIELD_ID+"=?";
        String[] whereValue={Integer.toString(id)};
        ContentValues cv=new ContentValues();
        cv.put(FIELD_TITLE, Title);
        db.update(TABLE_NAME, cv, where, whereValue);
       
       
    Cursor cursor = db.query("user", new String[]{"id","name"}, " id =?", new String[]{"1"}, null, null, null);
    String[] columns={"ID", "inventory"};
    String[] parms={"snicklefritz"};
    Cursor result=db.query("widgets", columns, "name=?",parms, null, null, null);
    Cursor c=db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='mytable'", null);
    Cursor c = db.rawQuery("SELECT * FROM table WHERE android123=?", args)
   
五:
    防止sql注入的最好方法 使用占位符  ?
   
    

你可能感兴趣的:(android,数据库,sqlite,移动开发,sqlite操作)