I am developing one application. In that iam facing the problem at inserting the data into database. First insert and update will be performed very well. After updating if i want to perform insert operation then app will be crashed. My code for inserting and updating were like below
+(BOOL)update:(CalendarInfo*)clInfo
{
NSString *query = [NSString stringWithFormat:#"UPDATE ABC set A = '%#' where B =%d and C=%d",clInfo.a,clInfo.b,clInfo.c];
sqlite3_stmt *stStatement;
if(sqlite3_prepare_v2(database, [query UTF8String], -1, &stStatement, nil)==SQLITE_OK)
{
if(SQLITE_DONE == sqlite3_step(stStatement))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
NSLog(#"updation Successful");
}
return 0;
}
+(BOOL)insert:(CalendarInfo*)clInfo{
sqlite3_stmt *addStmt = nil;
sqlite3 *contactDB;
NSArray *docPathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *destPath = [NSString stringWithFormat:#"%#/example.sqlite",[docPathArr objectAtIndex:0]];
if (sqlite3_open([destPath UTF8String], &contactDB)==SQLITE_OK) {
NSString *query2 = [NSString stringWithFormat:#"INSERT INTO ABC(C,B,A) VALUES(%d,%d,'%#')",clInfo.c,clInfo.b, clInfo.a];
if(sqlite3_prepare_v2(database, [query2 UTF8String], -1, &addStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
if(SQLITE_DONE != sqlite3_step(addStmt))
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
NSLog(#"Insertion Successful");
sqlite3_reset(addStmt);
return 0;
}
So please tell me how to solve my problem.
From the limited code you've posted, I think the answer has to do with that you have two different sqlite3 instances here. Was this intentional?
You call sqlite3_open([destPath UTF8String], &contactDB);
and then attempt to get an error result off another sqlite instance:
sqlite3_errmsg(database)
Either use database or contactDB and you should be all set.
Related
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 :(
I am trying to insert a set of values in an sqlite table using a for loop. It is inserting only one set of value. I am posting here my code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:#"myDatabase.sql"];
for(int i=0;i<[arr count];i++)
{
sqlite3 *database;
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(#"\n inserting data \n");
sqlite3_exec(database, [[NSString stringWithFormat:#"INSERT INTO AnswerConnect VALUES('%#')",[arr objectAtindex:i] ] UTF8String], NULL, NULL, NULL);
//sqlite3_finalize(compiledStatement);
sqlite3_close(database);
}
}
Thanks in advance.
You have to first prepare a sqlite statement to insert data in table.Try this :
sqlite3_stmt *statement = nil
const char *sql = "insert into tablename (col1,col2) Values( ?, ?)";
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSLog(#"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
for(int i=0;i<[arr count];i++)
{
sqlite3_bind_text(statement, 1,[[arr objectAtindex:i] UTF8String] , -1, SQLITE_TRANSIENT);
if(SQLITE_DONE != sqlite3_step(add_statement))
{
NSLog(#"Error while inserting result data. '%s'", sqlite3_errmsg(database));
}
//Reset the add statement.
sqlite3_reset(statement);
}
Don't do like that! Don't open/close SQLite connection in loop like that! Open handle to database outside from loop and than just use pointer on it. In this kind of request it's unsafe to insert format, because SQL statement may be compiled with some kind of injection code. Use sqlite3_stmt instead and bind values to it. Also if you compile only one instance of sqlite3_stmt and reuse it, this will give you better performance than compiling new statements all the time.
How many columns in each data set? Does it insert only one value from single data set like string?
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.
I am trying to insert a value to a SQLite db, but everytime I try my program simply crashes with no error message at all.
Here is my code:
- (void) insertToDatabase:(NSString *) refName {
// The Database is stoed in the application bundle
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"staticdata.sqlite"];
if(sqlite3_open([path UTF8String], &database) == SQLITE_OK){
const char *sql = "Insert into usersDates(dateDescription) VALUES (?)";
sqlite3_stmt *init_statement;
sqlite3_bind_text(init_statement, 1, [refName UTF8String], -1, SQLITE_TRANSIENT);
if(!sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) == SQLITE_OK){
NSAssert1(0, #"Failed to insert to database file with message '%s'.", sqlite3_errmsg(database));
}
if(sqlite3_step(init_statement) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(init_statement);
} else {
sqlite3_close(database);
NSAssert1(0, #"Failed to open database file with message '%s'.", sqlite3_errmsg(database));
}
}
The error seems to occur on the bind statement. I have confirmed the database is actually being opened, and refname is correctly being passed to my method.
Can anyone help? I would normally use core data, however this is a bug fix to an existing project, and I simply do not have the time to allocate to making the move to core data.
The order of your statements is incorrect. bind_() is used after prepare()
SQLite bind() documentation
The first argument to the sqlite3_bind_*() routines is always a pointer to the sqlite3_stmt object returned from sqlite3_prepare_v2() or its variants.
const char *sql = "Insert into usersDates(dateDescription) VALUES (?)";
sqlite3_stmt *init_statement;
if(!sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) == SQLITE_OK){
NSAssert1(0, #"Failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_bind_text(init_statement, 1, [refName UTF8String], -1, SQLITE_TRANSIENT);
sqlite_step(init_statement);
I have an application that needs a database. The application is running fine in the simulator. But when i try to deploy it onto the iphone then it gives me the error that 'no such table animal'. Where is the problem? I am providing the code for better understanding
(void)viewDidLoad
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:#"AnimalDatabase.sql"];
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
const char *sqlStatement = "insert into animal (id, name) VALUES (?, ?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
//NSLog(filePath);
sqlite3_bind_int(compiledStatement, 1, 12);
//NSLog(#"A");
sqlite3_bind_text( compiledStatement, 2, [#"abc" UTF8String], -1, SQLITE_TRANSIENT);
//NSLog(#"B");
if(sqlite3_step(compiledStatement) != SQLITE_DONE )
{
//NSLog(#"C");
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}
else
{
//NSLog(#"D");
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
}
}
else
{
NSAssert1(0, #"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
}
else
{
NSLog(#"Error Occured");
}
sqlite3_close(database);
[super viewDidLoad];
}
I think you need to look at what is being transfered to your iPhone. I had issues with this, there are some strange issues around how the database is created or not created on the actual iPhone. I think what is happening in your case is that no database is being transfered, and then your call to
sqlite3_open
is actually creating the database so you don't receive an error until you call a select statement.
Check your documentation on where to place the db in your resources to ensure it is copied to your iPhone when building.
The most likely reason is that the AnimalDatabase.sql file doesn't exist so there is no Animal table to insert into.