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.
-
1Only masochists use the SQLite C API in Objective-C. Use FMDB instead.Dave DeLong– Dave DeLong2011年02月21日 18:54:34 +00:00Commented Feb 21, 2011 at 18:54
-
@Dave Delong Not what I was looking for, I do not need any more dependeciesAntwan van Houdt– Antwan van Houdt2011年02月21日 18:55:25 +00:00Commented 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. :)Dave DeLong– Dave DeLong2011年02月21日 19:02:48 +00:00Commented Feb 21, 2011 at 19:02
-
1@Dave DeLong if i wanted a wrapper I could use Core DataAntwan van Houdt– Antwan van Houdt2011年02月21日 19:04:17 +00:00Commented Feb 21, 2011 at 19:04
-
1That'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.Dave DeLong– Dave DeLong2011年02月21日 19:10:20 +00:00Commented Feb 21, 2011 at 19:10
1 Answer 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.
Comments
Explore related questions
See similar questions with these tags.