iphone sqlite problem: "out of memory" on sqlite3_prepare_v2 - iphone

I'm wondering if someone can help me understand what is wrong with this block of code. I never get past the sqlite3_prepare_v2 statement and the debugger says :
'NSInternalInconsistencyException', reason: 'Error while creating add statement. 'out of memory''
static sqlite3 *database = nil;
sqlite3_stmt *addStmt = nil;
if(addStmt == nil) {
NSLog(#"About to add start time...\n");
const char *sql = "INSERT INTO games_played(start) VALUES(?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
NSLog(#"add statement created successfully!\n");
}
NSLog(#"About to bind start time...\n");
sqlite3_bind_text(addStmt, 1, #"start time", -1, SQLITE_TRANSIENT);

I never opened the database...I think thats the problem.

It is not a bug, the problem is you never open the database. For example:
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)

I was also facing this problem, and solved it by reset the database for the sqlite statement.

Related

I'm having trouble reading data from a sqlite file in objective c

I've been trying to figure this out for a while, and the problem seems to be that the if statement at the bottom of the following code is failing:
sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
}
else
NSLog(#"Database opened successfully");
//CREATE THE TABLE HERE IF NEEDED
NSString *createSQL = #"CREATE TABLE IF NOT EXISTS workouts "" (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";
char *errorMsg;
if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database);
NSAssert(0, #"Error creating table: %s", errorMsg);
}
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
-1, &statement, nil) == SQLITE_OK)
What could be the problem? I know there are a lot of other factors involved, but anything would be helpful. Just ask if you want any additional info. Thanks StackOverflow!
edit: the error that i'm receiving is: no such column: columnName
Honestly I wouldn't recreate any part of the Core Data framework unless you have a very good reason to do so. This tutorial might help you get started with Core Data programming
http://www.raywenderlich.com/934/core-data-on-ios-5-tutorial-getting-started
Here's the reference page for Core Data:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdBasics.html#//apple_ref/doc/uid/TP40001650

Xcode memory leak when using SQLite3 API

i am using the following code snippet to add new element/row with name to Sqlite3 database, Every thing works fine, But this is giving memory leaks each time, when i call this function, Can any one help me how to avoid this problem?
{
sqlite3 *database;
sqlite3_stmt *addStmt;
NSString *localdescription=#"Enter your Notes here";
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "insert into database(name) Values(?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_text(addStmt, 1, [localName UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
NSLog(#"id=====%d",sqlite3_last_insert_rowid(database));
//Reset the add statement.
sqlite3_reset(addStmt);
}
}
sqlite3_close(database);
}
You should finalize any prepared statement that you no longer use:
sqlite3_finalize(addStmt), addStmt = nil;
There is no real need to set the pointer to nil I just real like it.

how can i update data in table using sqlite in obj c

hii every one
can any one suggest me one good tutorial which shows update table in sqlite database
Open your database with:
sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK;
To update a row:
#synchronized(sqlLock]) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [[NSString stringWithFormat:#"UPDATE TABLE Set Text = ? where ID = '%#'",objectID] UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_bind_text(compiledStatement, 1, [text UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(compiledStatement))
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
sqlite3_reset(compiledStatement);
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}

sqlite3 is not working in my iphone application

I want to use sqlite3 for my iphone application. But it is not working. Scenario is :- On click event of a button a method should run and retrieve the data from the database. But My sequence of program is not going into
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select * from records";
sqlite3_stmt *compiledStatement;
0NSLog(#"%s", sqlite3_errmsg(database)); // Results - no error
-
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
//here NSLog statement does not work.
There is no problem in opening a database. What should be the reason? Any solution appreciable.
Thanks
Try this: NSLog(#"%s", sqlite3_errmsg(database)); and see if there are some errors in your query.
Here is a modified example of my own code :
NSString *file = #"youdatabase.db"
sqlite3 *db;
if(sqlite3_open([file UTF8String], &db) == SQLITE_OK)
{
NSString *req = #"SELECT * FROM totoTable";
sqlite3_stmt *returnReq;
if(sqlite3_prepare(db, [req cStringUsingEncoding:NSUTF8StringEncoding], -1, &returnReq, NULL) == SQLITE_OK)
{
while (sqlite3_step(returnReq) == SQLITE_ROW)
{
//Printf first returned value
NSLog(#"%s", (char*)sqlite3_column_text(returnReq, 0));
}
}
sqlite3_close(db);
}
Hope it will help you ;-)

sql won't add new entries. :(

My function won't add any entry to my existing sql database. Any ideas?
sqlite3 *database;
sqlite3_stmt *compiledStatement;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *tmpSQLStatement = [NSString stringWithFormat:#"INSERT INTO data (test) VALUES ('teststring');"];
const char *sql = [tmpSQLStatement UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
No ERRORMESSAGE Is called. But unfortunately nothing is added.
After sqlite3_prepare_v2, you need to actually execute the statement by calling sqlite3_step. Values should be inserted by then.