i am trying to insert into sqlite database using objective c,
the following is my code:
-(void)insertCustomer:(CustomerDetails *)cd{
CustomerDetails *cDetails=[[CustomerDetails alloc] initWithIdNumber:cd.idNumber name:cd.name surname:cd.surname];
NSString *name= cDetails.name;
NSString *surname=cDetails.surname;
NSString *idNumber=cDetails.idNumber;
NSString *insertQuery=[NSString stringWithFormat:@"INSERT INTO CUSTOMER_DETAILS(Name,Surname,ID_Number) VALUES(?,?,?);"];
sqlite3_stmt *insertStatement;
sqlite3_prepare_v2(database, [insertQuery UTF8String], 1, &insertStatement, NULL);
sqlite3_bind_text(insertStatement, 1,[name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2,[surname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 3,[idNumber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_step(insertStatement);
sqlite3_finalize(insertStatement);
sqlite3_close(database);
}
this is not inserting anything, also when i debug insertStatement seems to be null
i am new to objective c so would appreciate any help 🙂
Thanks
The third parameter of
sqllite3_prepare_v2is the number of bytes to read from the SQL statement. Since you are passing 1, one byte is read, in other words you are trying to execute the SQL statement"I". This is not going to work, andsqllite_prepare_v2is probably failing and therefore not assigning anything toinsertStatement– but since you are not checking the return value for an error code, your code just continues on.You can pass -1 to have the prepare function read until the null terminator of the UTF8 string. In other words, this should work:
Also, you really should check for errors when using the sqllite API in your code; this will likely avoid or explain other issues in the future. According to the docs: