4

this is how i try to add data in sqlite database everything works perfectly but data is not getting added in database i dont understand why can you please refer the code and tell me what am i doing wrong

    sqlite3_stmt *stmt;
    int x;

    char *update = "insert into PersonNamesAndBirthDates (Names,Birthdates,Phonenumber,Email,Profilepic) values(?,?,?,?,?);";
    x = sqlite3_prepare_v2(database1, update, -1, &stmt, nil);

    if (x == SQLITE_OK)
    {
        NSLog(@"PersonName is -->%@",[NSString stringWithFormat:@"%@",[_Namebarray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Birthdatebarray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:0]]);
        NSLog(@"BirthDates is -->%@",[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:0]]);
        // if else will come here for checking weather images has added or not if added then store Yes Image if not then simply No image for default image.


        sqlite3_bind_text(stmt, 1, [[NSString stringWithFormat:@"%@",[_Namebarray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 2, [[NSString stringWithFormat:@"%@",[_Birthdatebarray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 3, [[NSString stringWithFormat:@"%@",[_Phonearray objectAtIndex:0]] UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 4, [[NSString stringWithFormat:@"%@",[_Emailarray objectAtIndex:0]] UTF8String],-1, NULL);
        if(selectImage.image)
        {
            sqlite3_bind_text(stmt, 5, [[NSString stringWithFormat:@"%@",[_arrayOfPaths objectAtIndex:0]] UTF8String],-1, NULL);
        }
        else{
            sqlite3_bind_text(stmt, 5, [[NSString stringWithFormat:@"%@",@"No Image"] UTF8String],-1, NULL);
        }
    }
    if (sqlite3_step(stmt) != SQLITE_DONE){}
    NSLog(@"Error: ");
    sqlite3_finalize(stmt);
4
  • what do you mean with data is not getting added in database?, are you getting an error? Commented Apr 24, 2013 at 8:00
  • i am getting no error just database is not getting filled Commented Apr 24, 2013 at 8:00
  • where is you database file stored, in the app bundle or in the documents dir? Commented Apr 24, 2013 at 8:01
  • the NSLog calls are getting executed? Commented Apr 24, 2013 at 8:05

2 Answers 2

1

I see no error in your Code do check that your database is open like this

if (sqlite3_open(dbpath, &database1) == SQLITE_OK)
Sign up to request clarification or add additional context in comments.

1 Comment

Further, the return code really should be captured and the value logged if not SQLITE_OK, plus one should log the result of calling sqlite3_errmsg if not OK. Especially when becoming familiar with SQLite, one should make a point to check and log every "abnormal" return code.
0

There is an example project for using SQLite here: https://github.com/AaronBratcher/ABSQLite

It has code in there for adding and reading data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.