Xcode memory leak when using SQLite3 API - iphone

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.

Related

issue during insert data by using sqlite3 in to db iPhone

I have an issue where I am trying to insert data from NSMutableArray in to my database. Here is what I have so far:
- (void)insertList:(NSString*) strListName ArrayOfData:(NSMutableArray*) dataArray{
sqlite3 *database = nil;
sqlite3_stmt *addStmt = nil;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"customlists.sqlite"];
if(sqlite3_open([defaultDBPath UTF8String], &database) == SQLITE_OK)
{
for (int i = 0; i < dataArray.count; i++)
{
PageData *pageDta = [PageData new];
pageDta = [dataArray objectAtIndex:i];
if(addStmt == nil) {
const char *sql = "insert into NewList(ListName, PageName, PageNumber, id) Values(?, ?, ?, ?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [strListName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [pageDta.strNamebyFilter UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [pageDta.strPageNuber UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(addStmt, 4, i);
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
sqlite3_last_insert_rowid(database);
//Reset the add statement.
sqlite3_reset(addStmt);
}
}
else
{
NSLog(#"Error while opening database '%s'", sqlite3_errmsg(database));
}
sqlite3_close(database);
}
But after calling this function, the database is empty. Could someone help me to find the cause of this issue and potential remedies?
The main problem is simple - you are trying to write to a database in the app's bundle. This isn't allowed. An app's bundle is read-only on a real device (though it is writable in the simulator). The proper thing to do is copy the database from the resource bundle to a writable area of the app's sandbox the first time the app runs. Then every use of the database is done with the copy.
It would also be cleaner to prepare the statement before the for loop. No need to keep checking if it has been setup every loop iteration.
You make a call to sqlite3_last_insert_rowid but don't do anything with the value. What's the point?
You never finalize the statement after the loop is complete. You need to do that.
And you don't close the database when you are done.
change this line :
const char *sql = "insert into NewList(ListName, PageName, PageNumber, id) Values(?, ?, ?, ?)";
to :
NSString *insertSQL = [NSString stringWithFormat: #"NSString stringWithFormat: "INSERT INTO NewList(ListName, PageName, PageNumber, id) VALUES(\"%#\", \"%#\", \"%#\", \"%#\")" ,strListName, pageDta.strNamebyFilter, pageDta.strPageNuber, addStmt];
const char *sql = [insertSQL UTF8String];
I've rewrite function to:
if(sqlite3_open([defaultDBPath UTF8String], &database) == SQLITE_OK)
{
for (int i = 0; i < dataArray.count; i++)
{
PageData *pageDta = [PageData new];
pageDta = [dataArray objectAtIndex:i];
if(addStmt == nil) {
const char *sql = [[NSString stringWithFormat:#"INSERT INTO NewList (ListName, PageName, PageNumber) values('%#', '%#', '%#')",strListName,pageDta.strNamebyFilter, pageDta.strPageNuber] UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(addStmt, 1, [strListName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [pageDta.strNamebyFilter UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [pageDta.strPageNuber UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
//Reset the add statement.
sqlite3_reset(addStmt);
}
sqlite3_finalize(addStmt);
}
else
{
NSLog(#"Error while opening database '%s'", sqlite3_errmsg(database));
}
sqlite3_close(database);
but result is the same :(

Update table SQLIte in my iPhone App

I want to make it able in my app to insert data to sqlite database, and if the id for example is exist so to update this row.
i done the create with :
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *insertStmt = nil;
if(insertStmt == nil)
{
const char *insertSql = "INSERT INTO Category (id,name) VALUES(?,?)";
if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_int(insertStmt, 1, 135);
sqlite3_bind_text(insertStmt, 2, [#"fds" UTF8String], -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(insertStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
//NSLog("Inserted");
//Reset the add statement.
sqlite3_reset(insertStmt);
insertStmt = nil;
}
sqlite3_close(database);
Use the OR REPLACE qualifier:
INSERT OR REPLACE INTO Category ...
This relies on the existence of a UNIQUE constraint (usually the primary key) containing just the id column.
You can use following code for update:
-(void) Update:(NSString *)query {
sqlite3_stmt *statement=nil;
NSString *path = [self GetDatabasePath:DataBaseName] ;
if(sqlite3_open([path UTF8String],&databaseObj) == SQLITE_OK)
{
if(sqlite3_prepare_v2(databaseObj, [query UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_step(statement);
}
sqlite3_finalize(statement);
}
if(sqlite3_close(databaseObj) == SQLITE_OK){
}else{
NSAssert1(0, #"Error: failed to close database on memwarning with message '%s'.", sqlite3_errmsg(databaseObj));
}
}

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);
}

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.

iphone sqlite problem: "out of memory" on sqlite3_prepare_v2

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.