0

I am implementing some code for reading and writing the an sqlite3 database. Reading the database works fine, displaying the row is not a problem at all. The problem is in inserting a new row. For some reason it just does not work, sqlite does not return an error after executing the SQL.

if( sqlite3_prepare_v2(db, "INSERT INTO `items` (`itemID`, `itemName`) VALUES (2, 'helloWorld?')", -1, &statement, NULL) != SQLITE_OK)
 {
 NSAssert1(0, @"Error, unable to modify database",sqlite3_errmsg(db));
 }

db and statement:

sqlite3 *db = [RootViewController newDatabaseConnection];
 sqlite3_stmt *statement = nil;

Also I do know there is a framework called Core data, so please no answers saying use core data

The database file "AppName.sqlite3" is not located in the app's mainfolder, but in the documents folder so it should be editable

My SQL syntax is ok, tried adding the item using the command line. Everything works fine there.

asked Feb 21, 2011 at 18:51
5
  • 1
    Only masochists use the SQLite C API in Objective-C. Use FMDB instead. Commented Feb 21, 2011 at 18:54
  • @Dave Delong Not what I was looking for, I do not need any more dependecies Commented Feb 21, 2011 at 18:55
  • My point is that FMDB is well tested and commonly used; it ends up removing a LOT of boilerplate code from your source. And it's not really "another dependency"; it's simply a wrapper to make things easier. Also, I know this isn't the answer you were looking for, which is why I added it as a comment and not as an answer. :) Commented Feb 21, 2011 at 19:02
  • 1
    @Dave DeLong if i wanted a wrapper I could use Core Data Commented Feb 21, 2011 at 19:04
  • 1
    That's an option, sure. I only use CoreData if I'm constructing a brand new object graph. If I have an existing SQLite db, nothing beats the convenience or ease-of-use of FMDB. Commented Feb 21, 2011 at 19:10

1 Answer 1

1

As far as I'm aware, preparing a statement doesn't actually execute it. You need to call sqlite3_step() and pass the prepared statement in order for the SQL to be executed.

A a guide, you should prepare the statement and step through the results, then finalise the statement when you're done with it. Then when you're done with the db connection, you can close it.

answered Feb 21, 2011 at 19:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.