开发者

Insert date to sqlite

I am making an iphone app in which there is use of database. In this database I am trying to save the date which is selected by using datepicker. But whenever I go to insert query it get crash and the error it shows i.e. -[__NSDate UTF8String]: message sent to de allocated instance 0x5db1bf0. I am unable to resolve this error. Please give me some solution to insert the date into the sqlite.

Thankyou very much.

-(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 = n开发者_如何学Goil;

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

    }

}


My guess would be that you're trying to create an NSDate attribute, but that you're mistakenly trying to set this as an NSString, e.g. @"01-01-1999".

If you check your "Date And Time Programming Guide" in your Apple Docs, you'll fine examples of how to create dates using date components.

Also have a look on Stack Overflow, where you'll quickly find plenty of examples if you search under "Create NSDate from NSString", e.g.:

get NSDate from NSString

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜