查询:
SQLiteDatabase db= DBHelper.getDB();
//查询出来的数据顺序与SQLite里面的排列顺序相同
Cursor cursor=db.query("notice_info", null, null, null, null, null,null);
//查询出来SQLite里面数据按照ID由大到小排列
Cursor cursor=db.query("notice_info", null, null, null, null, null,"id DESC");
//根据某条字段信息查询出数据,例根据Id查询某条数据
String ID=“110”;
Cursor cursor=db.query("notice_info",null,"id=?",new String[]{ID},null,null,null);
String Id=null;
String Status=null;
String Title=null;
String WordTitle=null;
String WordPath=null;
String Content=null;
String CreateTime=null;
if(data.size()>0){
data.clear();
}
while (cursor.moveToNext()){
Id=cursor.getString(cursor.getColumnIndex("id"));
Status=cursor.getString(cursor.getColumnIndex("status"));
Title=cursor.getString(cursor.getColumnIndex("title"));
WordTitle=cursor.getString(cursor.getColumnIndex("wordTitle"));
WordPath=cursor.getString(cursor.getColumnIndex("wordPath"));
Content=cursor.getString(cursor.getColumnIndex("content"));
CreateTime=cursor.getString(cursor.getColumnIndex("createTime"));
}
添加:
SQLiteDatabase db= DBHelper.getDB();
for(int i=0;i<list.size();i++){
Notice notice=list.get(i);
String status="未读";
int id= Integer.parseInt(notice.getId());
String title=notice.getTitle();
String wordTitle=notice.getWordTitle();
String wordPath=notice.getWordPath();
String content=notice.getContent();
String createTime=notice.getCreateTime();
ContentValues contentValues=new ContentValues();
contentValues.put("id",id);
contentValues.put("status",status);
contentValues.put("title",title);
contentValues.put("wordTitle",wordTitle);
contentValues.put("wordPath",wordPath);
contentValues.put("content",content);
contentValues.put("createTime",createTime);
db.insert("notice_info", null, contentValues);
}
修改:
//根据ID从数据库中查出需要传递的数据
SQLiteDatabase db= DBHelper.getDB();
Cursor cursor=db.query("notice_info",null,"id=?",new String[]{ID},null,null,null);
System.out.println("<<<<<<"+position);
String WordTitle="";
String WordPath="";
String Title="";
String Content="";
while (cursor.moveToNext()){
WordTitle=cursor.getString(cursor.getColumnIndex("wordTitle"));
WordPath=cursor.getString(cursor.getColumnIndex("wordPath"));
Title=cursor.getString(cursor.getColumnIndex("title"));
Content=cursor.getString(cursor.getColumnIndex("content"));
//将查出来的这条数据的状态改为已读,然后插入数据库
ContentValues contentValues = new ContentValues();
//将要改的字段和要改的内容放放到contentValues
contentValues.put("status","已读");
//表明 要改的字段和要改的内容 要改哪条数据(此处要改id=ID的那条数据)
long dd=db.update("notice_info",contentValues, "id=?", new String[]{String.valueOf(ID)});
System.out.println("<<<<<<<"+dd+"<<<<"+ID);
}
删除:
SQLiteDatabase db = DBHelper.getDB();
//删除表名为“notice_info”里面的所有数据
db.delete("notice_info",null,null);
//删除id=ID的那一条数据
db.delete("notice_info",null,"id=?",new String[]{ID},null,null,null);