Fmdb reading a DateTime type value of 1970

Source: Internet
Author: User

1. Questions

Today, using Fmdb to do an example program, a new table has a DateTime field, the database has a default value, presumably as follows

CREATE  TABLE [Consumptiontype] ([id] INTEGER PRIMARY KEY not  NULL, [name] text,[level] Text,[creatdate] DATETIME Default (DateTime (' Now ', ' localtime '))

When I read the creatdate every time I get a value of 1970, I used to use string to save time stamp, did not find this problem

, the deletion of the table was done several times or not.

    Temp.creatdate=[rs dateforcolumn:@ "Creatdate"];//1970 this way read the time has been wrong

And then I read it into a string,

     Temp.creatdate=[rs stringforcolumn:@ "Creatdate"];//This is the right time to read it out

This means that the database access is correct, just read again when the problem, point into the source to see the discovery

The Fmdatabase class has a NSDateFormatter property, but when it is empty, it takes the default parsing time type

So if you want to properly handle data of the DateTime type, you need to pass a nsdateformatter to the db so that you can get

The correct value.

2. The correct code sample
/** * Get ALL consumer categories * * @return return all consumer classifications as an array */+ (nsarray*) getallconsumptiontype{__block Nsmutablearray * Arr=[[nsmuta    Blearray alloc] init];        __block Fmresultset *rs=nil; [[DBHelper Sharedfmdbmanager] indatabase:^ (fmdatabase *db)        {NSString * sql=[nsstring stringwithformat:@ "SELECT * from Consumptiontype"];        NSDateFormatter *outputformatter = [[NSDateFormatter alloc] init];        [Outputformatter Setlocale:[nslocale Currentlocale];        [Outputformatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];                [DB Setdateformat:outputformatter];                Rs=[db Executequery:sql];            while ([Rs next]) {Consumptiontype * Temp=[[consumptiontype alloc] init];            Temp.id=[rs intforcolumn:@ "id"];            Temp.name=[rs stringforcolumn:@ "name"];            Temp.level=[rs intforcolumn:@ "level"];            Temp.creatdate=[rs dateforcolumn:@ "Creatdate"];        [Arr addobject:temp];    }            }]; return arr;}

Fmdb reading a DateTime type value of 1970

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.