Reputation: 21
I have a problem with the inserting and retrieving code
There is no run time error but inserting data is not working and the retrieving code only retrieves the last record from the DB.
Is it better to use NSString
or NSMutableString
?
NSLog(@"test");
NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO User Values (2,'%@','','F','123','','','','','A','B','123','123',2)",name.text];
char *err;
sqlite3_exec(database, [sql1 UTF8String], NULL, NULL, &err)!= SQLITE_OK ;
const char *sql = "select * from User";
sqlite3_stmt *searchStatement;
if (sqlite3_prepare_v2(database, sql, -1, &searchStatement, NULL) == SQLITE_OK)
{
while (sqlite3_step(searchStatement) == SQLITE_ROW)
{
char * try = (char*)sqlite3_column_text(searchStatement, 1);
if (try){
item = [NSMutableString stringWithUTF8String:try];
}
else{
item = @"";
}
Upvotes: 2
Views: 145
Reputation: 20575
The issue seems to be in your retrieval code. The sqlite3_column_type methods use zero-based indexing, but it looks like you are starting from position 1. Thus each time you step through, you aren't pulling out the information you need. I'm not sure why you are getting the last item, but try going from position 0 and see if that helps.
Upvotes: 1