sqlite 有日期字段

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() )});
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无v邪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值