package com.lehua.cf.mirrormagic.sql;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.lehua.cf.mirrormagic.bean.Note;
import java.util.ArrayList;
import java.util.List;
public class TableNote {
public static final String TABLE_NAME="note";
public static void init(SQLiteDatabase db){
String sql = "create table "+TABLE_NAME+"(noteId integer PRIMARY KEY AUTOINCREMENT,note text,noteDate text ,noteTime text)";
db.execSQL(sql);
}
public static List<Note> queryAllNotes(SQLiteDatabase db){
String[] columns=new String[]{"noteID,note,noteDate,noteTime"};
Cursor cursor=db.query(TABLE_NAME,columns,null,null,null,null,"date(noteDate),time(noteTime) asc",null);
if(cursor==null )
return null;
ArrayList<Note> notes=new ArrayList<>();
Note note;
while (cursor.moveToNext()){
note=new Note();
note.setNoteId(cursor.getInt(0));
note.setNote(cursor.getString(1));
note.setDate(cursor.getString(2));
note.setTime(cursor.getString(3));
notes.add(note);
}
return notes;
}
public static List<Note> queryNotesByDate(SQLiteDatabase db,Note _note){
String[] columns=new String[]{"noteID,note,noteDate,noteTime"};
Cursor cursor=db.query(TABLE_NAME,columns,"date(noteDate)=?",new String[]{_note.getDate()},null,null," time(noteTime) asc",null);
if(cursor==null ) {
return null;
}
ArrayList<Note> notes=new ArrayList<>();
Note note;
while (cursor.moveToNext()){
note=new Note();
note.setNoteId(cursor.getInt(0));
note.setNote(cursor.getString(1));
note.setDate(cursor.getString(2));
note.setTime(cursor.getString(3));
notes.add(note);
}
return notes;
}
public static long addNote(SQLiteDatabase db,Note note){
ContentValues cv = new ContentValues();
cv.put("note",note.getNote());
cv.put("noteDate", note.getDate());
cv.put("noteTime", note.getTime());
return db.insert(TABLE_NAME,null,cv);
}
public static int delNote(SQLiteDatabase db,Note note){
return db.delete(TABLE_NAME,"noteId=?",new String[]{String.valueOf( note.getNoteId() )});
}
}
sqlite 有日期字段
最新推荐文章于 2025-10-14 17:58:23 发布
本文深入解析了使用SQLite数据库进行数据操作的方法,包括创建表格、查询所有记录、按日期查询记录、添加新记录以及删除记录的具体实现。通过示例代码展示了如何在Android环境下,利用SQLiteDatabase进行高效的数据管理。

4488

被折叠的 条评论
为什么被折叠?



