background image

游标的数据集(与上面的语句最大的区别就是防止 SQL 注入)
数据的添删改查分别可以通过两种途径来实现。
数据的添加
1.使用 insert 方法

39 view sourceprint?1ContentValues cv = new ContentValues();//实例化一个 ContentValues
用来装载待插入的数据 cv.put("username","Jack Johnson");//

 

添加用户名

40

 

41 2cv.put("password","iLovePopMusic"); //

 

添加密码

42

 

43 3db.insert("user",null,cv);//

 

执行插入操作

2.使用 execSQL 方式来实现

44 view   sourceprint?1String   sql   =   "insert   into   user(username,password)   values   ('Jack 
Johnson','iLovePopMuisc');//插入操作的 SQL

 

语句

45

 

46 2db.execSQL(sql);//执行 SQL

 

语句

数据的删除同样有 2 种方式可以实现

47 view sourceprint?1String whereClause = "username=?";//

  

删除的条件

48

  

49 2String[] whereArgs = {"Jack Johnson"};//

  

删除的条件参数

50

  

51 3db.delete("user",whereClause,whereArgs);//

  

执行删除

52

  

53 //使用 execSQL

  

方式的实现

54

  

55 view sourceprint?1String sql = "delete from user where username='Jack Johnson'";//删除
操作的 SQL

  

语句

56

  

57 2db.execSQL(sql);//

  

执行删除操作

数据修改同上,仍是 2 种方式

58 view sourceprint?1ContentValues cv = new ContentValues();//实例化 ContentValues 
59

 

60 2cv.put("password","iHatePopMusic");//

 

添加要更改的字段及内容

61

 

62 3String whereClause = "username=?";//

 

修改条件

63

 

64 4String[] whereArgs = {"Jack Johnson"};//

 

修改条件的参数

65

 

66 5db.update("user",cv,whereClause,whereArgs);//

 

执行修改

67

 

68 //使用 execSQL

 

方式的实现

69

 

70 view  sourceprint?1String  sql  =  "update  [user]  set   password  =  'iHatePopMusic'   where 
username='Jack Johnson'";//修改的 SQL

 

语句

                     找软件资料,就到一览软件文库

http://wk.yl1001.com/rj/