1

I am running code from this tutorial sqlite database iOS app tutorial .The query function is :

- (IBAction)saveInfo:(id)sender
{
    //  Prepare the query string.
    //  If the recordIDToEdit property has value other than -1, then create an update query. Otherwise create an insert query.
    NSString *query;

    if (self.recordIDToEdit == -1)
    {
        query = [NSString stringWithFormat:@"insert into peopleInfo values(null, '%@', '%@', '%@')", self.txtFirstname.text, self.txtLastname.text, self.txtAge.text ];
    }
    else
    {
        query = [NSString stringWithFormat:@"update peopleInfo set firstname='%@', lastname='%@', age='%@' where peopleInfoID=%d", self.txtFirstname.text, self.txtLastname.text, self.txtAge.text, self.recordIDToEdit];
    }



    NSLog(@"The query is: %@",query);

    //  Execute the query.
    [self.dbManager executeQuery:query];

    //  If the query was succesfully executed then pop the view controller.
    if (self.dbManager.affectedRows != 0) {
        NSLog(@"Query was executed successfully. Affacted rows = %d", self.dbManager.affectedRows);

        //  Inform the delegate that editing was finished.
        [self.delegate editingInfoWasFinished];

        //  Pop the view controller.
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        NSLog(@"Could not execute the query");
    }
}

Program is giving sql error on inserting record

The query is: insert into peopleInfo values(null, 'Project 1', 'Test 1', '2014-12-15 05:00:20 +0000')
DB Execute Query Error: SQL logic error or missing database
Could not execute the query.

How to get remove this error ?

7
  • What do you mean about null in insert query. Is that column is identity or just a integer? Check whether the column is not null. Is it working with update query? Commented Dec 16, 2014 at 5:22
  • Have you imported the Database files folder (which comes with the code which you refered) into your project? You probably should have missed it. Cuz the error shows missing database. Commented Dec 16, 2014 at 5:39
  • Have you specified the database path in the application Commented Dec 16, 2014 at 5:50
  • how to specify it? i have done it using this command: self.dbManager = [[DBManager alloc] initWithDatabaseFileName:@"db.sql"];in viewdid load Commented Dec 16, 2014 at 5:54
  • @frincit: yes, thats correct after that you have to copy file to the documents directory Commented Dec 16, 2014 at 6:20

1 Answer 1

1

enter image description here

See the highlighted folder. Copy that into your project. Your code seems to be fine. And make sure db.sql is in there.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok try resetting the simulator. Reset content and settings. And rerun the app. And are you running the sample file itself? or have you modified it on your own as well? Cuz I have downloaded the code and it runs perfectly here.
Idk. The sample file runs perfectly here.
Without further clue . We are stuck here lol. Are u running the sample code itself or have you tweaked the codes somewhere?

Your Answer

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