将日期插入sqlite

时间:2011-09-13 10:11:45

标签: iphone

我正在制作一个使用数据库的iphone应用程序。在这个数据库中,我试图保存使用datepicker选择的日期。但每当我去插入查询时它会崩溃并显示错误,即    -[__NSDate UTF8String]: message sent to de allocated instance 0x5db1bf0. 我无法解决此错误。请给我一些解决方案,将日期插入sqlite。

非常感谢。

-(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));
            }

    }

}

1 个答案:

答案 0 :(得分:0)

我的猜测是你正在尝试创建一个NSDate属性,但你错误地试图将其设置为NSString,e.g. @"01-01-1999".

如果您在Apple Docs中查看“日期和时间编程指南”,那么您将了解如何使用日期组件创建日期的示例。

另请查看Stack Overflow,如果您在“CreateNSDate NSString下搜索,您可以快速找到大量示例,例如:

get NSDate from NSString

相关问题