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