0

Hopefully my question has a simple solution. I am just starting to use sqlite3 and I have created a database called local and a table called table with 1 column called name with type text.

It connects to the database fine its just I am trying to input a string value into the tables column but I think there is an issue with my INSERT text. I know its probably something simple if anyone could just let me know where I went wrong. Thanks.

SString *inserStmt = [NSString stringWithFormat:@"INSERT INTO TABLE(NAME) VALUES ('%s')", [key UTF8String]];
const char *insert_stmt = [inserStmt UTF8String];

sqlite3_exec(database, insert_stmt, NULL, NULL, NULL);
3
  • 1
    Having a table called "table" is going to cause you all kinds of issues... Commented Feb 4, 2013 at 17:59
  • probably not an answer to your question but check out fmdb on github - will make your life a lot easier Commented Feb 4, 2013 at 18:01
  • @lc Yes I know its simply for testing purposes. Commented Feb 4, 2013 at 18:04

1 Answer 1

2

Your first problem is that you ignore any errors and don't display the error message, so if something goes wrong, you will have no clue.

Your second problem is that TABLE is a keyword; to use it as a name in SQL statements, you have to escape it with double quotes:

INSERT INTO "TABLE"(NAME) VALUES('...')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much that worked. I did have an alert setup to let me know whether it failed or not I just didn't include it in my post.

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.