Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了从android中的sqlite数据库中获取字符串,日期,时间和int大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用此代码将字符串,日期,时间和int值插入sqlite数据库

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

这是我的代码来检索它

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);
    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类的构造函数中的第4个参数是java.util.date类中的Date类型.

我的问题是“我如何设置光标以日期格式获取它?”

生病了很高兴根据要求提供额外的代码

最佳答案
由于您无法在sqlite中保存日期/时间值,因此应首先将它们转换为毫秒并将其存储为.

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

如果您需要将毫秒更改回日期格式:

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

然后,您可以使用SimpleDateFormat格式化日期.

大佬总结

以上是大佬教程为你收集整理的从android中的sqlite数据库中获取字符串,日期,时间和int全部内容,希望文章能够帮你解决从android中的sqlite数据库中获取字符串,日期,时间和int所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。