xcode 将日期保存到 SQLite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8165033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:27:11  来源:igfitidea点击:

Saving Date to SQLite

iphoneiosxcodesqlite

提问by Chandu

-(void)save:(id)sender
{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &expenseDB) == SQLITE_OK)
{

    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO ADDEXPENSE(date, description, category, amount) VALUES (\"%@\", \"%@\", \"%@\",\"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text , fieldFour.text];

    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(expenseDB, insert_stmt, -1, &statement, NULL);
    if (sqlite3_step(statement) == SQLITE_DONE)
    {
      status.text = @"saved"
    } 
    else 
    {
       status.text = @"Failed"
    }
    sqlite3_finalize(statement);
    sqlite3_close(expenseDB);
}
}

This Method Works Fine For Me.But How to Save it as Date instead of strings? I tried a few ways but didnt work for me. As i am new to coding.someone please help me with a piece of code u u u u u u u u u This is the date Picker i used.

这种方法对我来说很好。但是如何将其保存为日期而不是字符串?我尝试了几种方法,但对我不起作用。因为我是编码的新手。有人请帮我写一段代码 uuuuuuuuu 这是我使用的日期选择器。

UIDatePicker *picker = (UIDatePicker *)sender;
NSDate *dateSelected = [picker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd.MM.yyyy"];
self.fieldOne.text = [dateFormatter stringFromDate:dateSelected];

回答by simongking

SQLLite doesnt have a date datatype so you have to save it as either text or a number. I personally use the text method and my code converts any dates to text on insert or update and converts it back on a select.

SQLLite 没有日期数据类型,因此您必须将其保存为文本或数字。我个人使用 text 方法,我的代码在插入或更新时将任何日期转换为文本,然后在选择时将其转换回。

so to get from a date I use

所以从我使用的日期开始

[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *dateString = [self.dateFormatter stringFromDate:date];

To convert it back

将其转换回来

[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *theDate = [self.dateFormatter dateFromString:ds];

I keep the value as a date until the point I want to display it.

我将该值保留为日期,直到我想显示它为止。

Your code example needs some thought though as one of the key concepts in iOS development is MVC which means you should try and separate your View from your Model. Ideally you should have an object which has the data stored in it. Your View knows how to display the data and your Model knows how to store and retrieve the data.

您的代码示例需要一些思考,因为 iOS 开发中的关键概念之一是 MVC,这意味着您应该尝试将视图与模型分开。理想情况下,您应该有一个存储数据的对象。您的视图知道如何显示数据,您的模型知道如何存储和检索数据。

It's worth looking into as it makes code more maintainable and extendible.

值得研究,因为它使代码更易于维护和扩展。

回答by ankit.tlp

It would be better to create a field of double data type in the DB and the save the date in timestamp using [NSDate timeinterversince1970]; this will make the NSDate to timestamp and you can save it in the database in double formate, further while use the similar type of function in NSDate class to convert the timestamp to NSDate.

最好在数据库中创建一个双数据类型的字段,并使用 [NSDate timeinterversince1970] 将日期保存在时间戳中;这将使NSDate成为时间戳,您可以将其以双格式保存在数据库中,同时使用NSDate类中类似类型的函数将时间戳转换为NSDate。

回答by alloc_iNit

Usually date value is being stored as string. But there are few ways that allows a developer to store a date value except date.

通常日期值被存储为字符串。但是很少有方法可以让开发人员存储除日期之外的日期值。

Following are the references those will let you know about the same.

以下是那些会让您了解相同情况的参考资料。

  1. Persisting Dates to SQLite3 in an iPhone Application

  2. Objective-C and sqlite's DATETIME type

  1. 在 iPhone 应用程序中将日期保留到 SQLite3

  2. Objective-C 和 sqlite 的 DATETIME 类型

回答by pooja_chaudhary

Code to add date to sqlite database table:

将日期添加到 sqlite 数据库表的代码:



-(void) saveData



{



sqlite3_stmt *statement = NULL;

const char *dbpath = [databasePath UTF8String];
sqlite3_reset(statement);

if (sqlite3_open(dbpath, &recordsDB) == SQLITE_OK)

{



NSString* Date = [NSDate dateWithTimeIntervalSinceNow : sqlite3_column_double(statement, 0)];


    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO RECORDS Date VALUES ( \"%@\")",Date];


    NSLog(@"save Data %@",Date);
    NSLog(@"save Data %@",insertSQL);  //to print the values inserted in the database



    const char *insert_stmt = [insertSQL UTF8String];

    sqlite3_prepare_v2(recordsDB, insert_stmt, -1, &statement, NULL);


    if (sqlite3_step(statement) == SQLITE_DONE)
    {

           NSLog(@"@database created");

        NSLog(@"databasevalue:%@",insertSQL);


    } else
    {
        NSLog(@"@database not created");
    }

    sqlite3_finalize(statement);


    sqlite3_close(recordsDB);

}
}