SQl语句
增
String sql="insert into "+Constants.TABLE_NAME+" ( _id,name,age,salary,phone,address) values (?,?,?,?,?,?)"
db.execSQL(sql,new Object[]{1,"xiaocai",18,100,123,"USA"});
db.close();
删
String sql="delete from "+Constants.TABLE_NAME+" where age=18";
db.execSQL(sql);
db.close();
改
String sql="update "+Constants.TABLE_NAME+" set salary=200";
db.execSQL(sql);
db.close(); //关闭数据库
查
String sql="select *from "+Constants.TABLE_NAME;
Cursor cursor=db.rawQuery(sql,null);//需要有一个返回值显示查询内容
while(cursor.moveToNext()){
int index=cursor.getColumnIndex("name");
String name=cursor.getString(index);
Log.d("Dao","name is"+name);
}
cursor.close();//cursor一定也要关闭
db.close();
Cursor 游标
存储查询的结果
方法
-
getColumnCount()
返回所有列的总数
-
getCount()
返回Cursor 中的行数
-
getColumnName(int columnIndex)
-
getColumnIndex(String columnName)
返回指定列 ,如果不存在返回-1
-
moveToFirst()
移动光标到第一行
-
moveToLast()
移动光标到最后一行
-
moveToNext()
移动光标到下一行
-
moveToPosition(int position)
移动光标到一个绝对的位置
-
moveToPrevious()
移动光标到上一行