Android SQLite 按时间插入和排序

发布于 2024-11-01 20:23:40 字数 1101 浏览 0 评论 0原文

我正在编写一个 Android 应用程序,允许用户向数据库添加新模块,并添加该模块的日期、开始时间、完成时间和空间。

用户可以查看特定日期的所有课程。

我遇到的问题是,现在我正在将开始时间和结束时间作为文本,因为我不知道如何将其作为时间。

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

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

我一切正常,我面临的唯一问题是,当用户查看特定日期的所有课程时,我希望它按开始时间排序,即第一堂课、第二堂课等。但现在它只按照插入数据库的顺序显示它。我已经尝试过 ORDER BY 但它不起作用。

我猜我必须更改开始时间和结束时间的数据类型。

db.execSQL("CREATE TABLE AllModulesInfo (_id INTEGER PRIMARY KEY AUTOINCRMENT, moduleName TEXT, day Text, startTime Text, finishTime Text, room Text);");

    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);
    }

提前致谢,如果有什么问题的话没有正确解释或我遗漏的任何信息可以帮助您帮助我解决此问题,请告诉我。谢谢

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) 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) The text in the EditText is then taken and saved into the database.

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 TEXT, day Text, startTime Text, finishTime Text, room Text);");

    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);
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2024-11-08 20:23:40

看起来您正在使用字符串作为时间戳。您需要使用整数来实际存储 unix 时间戳。然后,您的查询将使用 ORDER BY 正确排序,例如:

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

我建议使用直接 Java 将字符串转换为整数,因为这允许您使用示例中所示的参数化插入。但是,您可以手动编写查询并使用 SQLite 提供的时间函数:

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


编辑:

看起来你可以在 SQLite 中使用字符串时间戳,但只支持几种格式(该页面有示例。)我不确定这在 Android 中是否有效,我已经总是使用整数字段类型来表示时间(其中实际上可以存储 Java long。)

——Dan

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

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


Edit:

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.)

-- Dan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文