得到的字符串,日期,时间和针对Android SQLite数据库诠释 [英] get string, date, time and int from sqlite database in android

查看:145
本文介绍了得到的字符串,日期,时间和针对Android SQLite数据库诠释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code插入一个字符串,日期,时间和int值到SQLite数据库

i inserted a string, date, time and int value into an sqlite database using this code

void addRemAction(RemActions remaction) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_ACTOINS_TITLE, remaction.getTitle()); // Action title
    values.put(KEY_ACTIONS_MSG, remaction.getMsg()); // action msg



    values.put(KEY_ACTIONS_DATE, dateFormat.format(new Date()));  // action date
    values.put(KEY_ACTIONS_TIME, remaction.getTime()); // action time
    values.put(KEY_ACTIONS_PLACE_LONG, remaction.getPlong()); // action place long
    values.put(KEY_ACTIONS_PLACE_LAT, remaction.getPlat());  // action place lat

    // Inserting Row
    db.insert(TABLE_ACTIONS, null, values);
    db.close(); // Closing database connection

这是我的code找回它

and this is my code to retrieve it

RemActions getRemAction(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_ACTIONS, new String[] {KEY_ACTOINS_TITLE, KEY_ACTIONS_MSG, KEY_PLACES_NAME, KEY_ACTIONS_DATE, KEY_ACTIONS_TIME}, KEY_ACTIONS_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    RemActions remActions = new RemActions(cursor.getString(0),
            cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4));
    // return remActions
    return remActions;

现在我的IDE是给我的错误,因为在我的构造方法RemAction类的第四个参数是从java.util.date类Date类型。

now my ide is giving me errors because the 4th argument in my constructor for the RemAction class is of type Date from the java.util.date class.

我的问题是我如何设置光标得到它的日期格式?

My question is "how do i set the cursor to get it in a date format?"

生病b乐意提供额外的code要求

ill b glad to provide additional code on request

推荐答案

既然你不能保存在SQLite的日期/时间值,你应该首先将它们转换成毫秒,并将它们存储作为​​。

Since you cannot save date/time values in SQLite, you should first convert them to milliseconds and store them as that.

Date c = new Date(System.currentTimeMillis());
long milliseconds = c.getTime();
//To get current time

如果您然后需要改变毫秒回日期格式:

If you then need to change milliseconds back to a date format:

Date c = new Date(cursor.getLong(4));

然后就可以使用格式化日期的SimpleDateFormat

这篇关于得到的字符串,日期,时间和针对Android SQLite数据库诠释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆