Android的SQLite的插入和按时间排序 [英] Android SQLite Inserting and Ordering by time

查看:591
本文介绍了Android的SQLite的插入和按时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,允许用户在一个新的模块添加到数据库,并添加的那一天,开始时间,完成该模块的时间和空间。

I am writing an Android app that allows the user to add a new Module to the database and add the day, starttime, finish time and room for that module.

用户能够查看所有类特定的一天。

The user is able to view all classes for a particular day.

我遇到的问题是,现在我目前正在服用的开始时间和结束时间为文本,因为我无法找到如何把它作为一个时间。

The problem i am having is, right now i am currently taking the start time and finish time as text because i couldnt find out how to take it in as a time.

1)的用户点击的EditText,一个TimePicker对话框弹出,用户选择的小时和分钟和的EditText文本设置为时间拾取

1) The user clicks the EditText, a TimePicker Dialog pops up, the user selects the hour and minute and the EditText text is set to the time picked.

2)的EditText中的文本,然后取并保存到数据库中。

2) The text in the EditText is then taken and saved into the database.

我所拥有的一切工作正常,我面对的唯一问题是,当用户观看所有类某一天我希望它由开始时间,即第一类,第二等订购。但现在其只显示它它被插入到数据库中的顺序。我曾尝试ORDER BY,它只是难道不工作。

I have everything working fine, the only problem i am facing is that when the user is viewing all the classes for a particular day i want it to be ordered by start time i.e. the first class, the second and so on. But right now its only showing it in the order it was inserted into the database. I have tried the ORDER BY and it just wouldnt work.

我猜我将不得不改变的开始时间数据类型和结束时间。

I am guessing i would have to change the data type of the start time and finish time.

db.execSQL(CREATE TABLE AllModulesInfo(_id INTEGER PRIMARY KEY AUTOINCREMENT,MODULENAME文本,文本一天,的startTime文本,文本finishTime,客房文本););

    public void insertIntoAllMod(String moduleName, String day, String startTime, String finishTime, String room)
    {
        ContentValues daysInfo = new ContentValues();
        daysInfo.put("moduleName", moduleName);
        daysInfo.put(Day, day);
        daysInfo.put(StartTime, startTime);
        daysInfo.put(FinishTime, finishTime);
        daysInfo.put(Room, room);           
        db.insert("AllModulesInfo", null, daysInfo);
    }

在此先感谢,如果有什么我还没有正确地解释或者我已经离开了,这将有助于你在assiting我解决这个问题的任何信息,请让我知道。谢谢

Thanks in advance, if there is anything i havent explained properly or any information i have left out that will assist you in assiting me to fix this problem, PLEASE let me know. Thanks

推荐答案

它看起来像你使用字符串的时间戳。您需要使用一个整数实际存储Unix时间戳。然后,你的查询将使用ORDER BY可以正确排序,例如:

It looks like you're using strings for your timestamps. You need to use an integer to actually store a unix timestamp. Then your queries will using ORDER BY will sort correctly, ex:

SELECT * FROM table WHERE .... ORDER BY updateTime

我建议将字符串转换为直接使用Java的一个整数,因为这可以让你像你在你的例子显示使用这些参数插入。但是,您可以手动编写查询并使用的SQLite提供的时间函数:

I'd suggest converting the string to an integer using straight Java, since that allows you to use those parameterized inserts like you showed in your example. However, you could manually write the queries and use the time functions provided by SQLite:

http://www.sqlite.org/lang_datefunc.html

编辑:

看样子你可以使用一个字符串时间戳和SQLite的,但只有少数格式的支持(即页面有例子。)我不知道,虽然在Android的工作,我一直使用整型字段类型倍(这实际上可以存储Java多头)。

It appears you can use a string timestamp with SQLite, but only a few formats are supported (that page has examples.) I'm not sure that works in Android though, I've always used integer field types for times (which can actually store Java longs.)

- 丹

这篇关于Android的SQLite的插入和按时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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