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 有日期字段
最新推荐文章于 2024-08-26 11:08:18 发布