2011-09-13 108 views
2

我正在製作一個iphone應用程序,其中有使用數據庫。在這個數據庫中,我試圖保存使用datepicker選擇的日期。但每當我去插入查詢它會崩潰,並顯示它的錯誤,即 -[__NSDate UTF8String]: message sent to de allocated instance 0x5db1bf0. 我無法解決此錯誤。請給我一些解決方案,將日期插入到sqlite中。插入日期到sqlite

非常感謝Thankyou。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if (btntagvalue == 2) { 
     if (buttonIndex != [actionSheet cancelButtonIndex]) { 
      selectedDate1 = [pickerView date]; 
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
      [formatter setDateFormat:@"yyyy-MM-dd"]; 
      frmdatestring = [formatter stringFromDate:selectedDate1]; 
      newfromdate = [[NSDate alloc]init]; 
      newfromdate = [[selectedDate1 addTimeInterval:-21600*4]copy]; 
      NSLog(@"newdateis:%@",newfromdate); 
      fromdate.text = frmdatestring; 
      NSLog(@"proper date:%@",fromdate.text); 

     } 
    } 
    else if (btntagvalue == 3) 
    { 
     if (buttonIndex != [actionSheet cancelButtonIndex]) { 
      NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
      [formatter setDateFormat:@"yyyy-MM-dd"]; 
      selectedDate2 = [pickerView1 date]; 
      LastDate = selectedDate2; 
      todatestring = [formatter stringFromDate:selectedDate2]; 
      todate.text = todatestring; 
      NSLog(@"proper date:%@",todate.text); 
      [self counteddays]; 
     } 
    } 
} 

-(IBAction)savedetails:(id)sender 
{ 
    docname = doctname; 
    mail = email; 
    contactnum = contact; 
    medicname = mediname; 
    medictype = meditype; 
    Lastdatecolmn = (NSString*)LastDate; 

    [self insert1]; 
    NSLog(@"docname:%@",docname); 
    NSLog(@"mail:%@",mail); 
    NSLog(@"contactnum:%@",contactnum); 
    NSLog(@"medicname:%@",medicname); 
    NSLog(@"medictype:%@",medictype); 

    for (int i =0; i<=[totaltimewithdate count]-1; i++) { 
    Dtime = [totaltimewithdate objectAtIndex:i];  
     [self insert];  
    } 
    [self docmedname]; 

} 

-(void)insert 
{ 
    sqlite3_stmt *addStmt = nil; 

    if(addStmt == nil) 
    { 
      const char *sql = "insert into medicationdetail(doctorname, emailid, contactno,medicationname, medicationtype, datetime,LastDateColmn) values (?,?,?,?,?,?,?)"; 

     //NSLog(@"sq; %@", sql); 
      if(sqlite3_prepare_v2(database, sql , -1, &addStmt, NULL) != SQLITE_OK) 
      { 
       NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database)); 
      } 

      sqlite3_bind_text(addStmt, 1, [docname UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(addStmt, 2, [mail UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(addStmt, 3, [contactnum UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(addStmt, 4, [medicname UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(addStmt, 5, [medictype UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(addStmt, 6, [Dtime UTF8String], -1, SQLITE_TRANSIENT); 
     sqlite3_bind_text(addStmt, 7, [Lastdatecolmn UTF8String], -1, SQLITE_TRANSIENT); 
      if(SQLITE_DONE != sqlite3_step(addStmt)) 
      { 
       NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database)); 
      } 

    } 

} 
+0

它會幫助,如果你能發佈你正在使用的代碼,這樣你會得到更有條理的響應。 –

+0

PLZ提供代碼.. – Maulik

+0

如果您鍵入DATETIME,則可以將該日期對象存儲爲字符串或雙精度。如果將其存儲爲雙精度值更好。 – Narayana

回答

0

我的猜測是,你想創建一個NSDate屬性,但你錯誤地試圖將此作爲一個NSString,e.g. @"01-01-1999".

如果您檢查您的「日期和時間規劃指南「在你的蘋果文件中,你會很好的使用日期組件創建日期的例子。

也對堆棧溢出,你很快就會發現大量的例子,如果你「CreateNSDateNSString」搜索下,比如一看:

get NSDate from NSString