i'm trying to insert some data on a database using this code:
-(void)insertLocationOnDatabase:(LocationType *)aLocation {
sqlite3_stmt *stmt;
int location = [aLocation.locationID intValue];
NSLog(#"Location ID: %i", location);
const char *sql = "insert into tbl_location values (?,?,?,?)";
if (sqlite3_prepare_v2(database, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(stmt, 0, location);
sqlite3_bind_text(stmt, 1, [aLocation.Description UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [aLocation.isActive UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, [aLocation.sequenceOrder UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(#"Location %# inserted on database",aLocation.Description);
}
else {
NSLog(#"Error on step: %i",sqlite3_errcode(database));
}
}
else {
NSLog(#"Error on prepare: %i",sqlite3_errcode(database));
}
}
the problem is on line:
sqlite3_bind_int(stmt, 0, location);
without this line and changing the sql, the code works fine, but when i put this line back, i get this error:
2010-09-17 10:24:01.032 StockControl[944:207] Error on step: 20
From sqlite3.h:
#define SQLITE_MISMATCH 20 /* Data type mismatch */
Somebody knows where is my mistake?
Regards,
Claudio
According to the docs for the binding methods in SQLite, the bindings count from one, not zero:
The second argument is the index of
the SQL parameter to be set. The
leftmost SQL parameter has an index of
1.
That might well lead to a type mismatch.
Why not use following methods?
NSString *sqlCmd = [NSString stringWithFormat:
#"insert into tbl_location values (%d,'%#','%#','%#')",
location,
aLocation.Description,
aLocation.isActive,
aLocation.sequenceOrder];
const char *sql = [sqlCmd UTF8String];
// ...
I use bind only for large data, for example an image file.
Related
My application crashed when i insert the data in SQLite Database.
My code of Inserting Data in SQLite Database is
for (int i=0; i<[arrNewFriendsId count]; i++) {
NSString *stQuery=[NSString stringWithFormat:#"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%#','%#')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];
NSLog(#"Query registered NewFriendsTable : %#",stQuery);
if (sqlite3_open([appdelegate.databasePath UTF8String], &database) == SQLITE_OK) {
const char *sql =[stQuery cStringUsingEncoding:NSASCIIStringEncoding];
sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL);
if(sqlite3_step(selectstmt)==SQLITE_DONE)
{
sqlite3_bind_text(selectstmt, 1, [[arrNewFriendsId objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectstmt, 3, [[arrNewFriendsFullName objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
NSLog(#"insert successfully NewFriendsTable ");
}
else
{
NSLog(#"insert not successfully NewFriendsTable ");
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
}}
In above code the app got crashed on this line
sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL);
The Data Inserted is Coming From FaceBook and the Insert Query is
INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('100000564063540','Rãhüł Pâtęł')
I don't get the problem.
I think it's related to Rãhüł Pâtęł. Because it's not regular character.
but then also don't know that this is the exact problem.
any advice, tutorial, code, link will be great help.
Issue is within your code.
Why you are using:
sqlite3_bind_text(selectstmt, 1, [[arrNewFriendsId objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectstmt, 3, [[arrNewFriendsFullName objectAtIndex:i] UTF8String], -1, SQLITE_TRANSIENT);
Because you already have the values in the query itself.
You need to remove those statements.
Use like:
for (int i=0; i<[arrNewFriendsId count]; i++)
{
NSString *stQuery=[NSString stringWithFormat:#"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%#','%#')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];
NSLog(#"Query registered NewFriendsTable : %#",stQuery);
if (sqlite3_open([appdelegate.databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql =[stQuery UTF8String];
if(sqlite3_prepare_v2(database,sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
if(sqlite3_step(selectstmt)==SQLITE_DONE)
{
NSLog(#"insert successfully NewFriendsTable ");
}
else
{
NSLog(#"insert not successfully NewFriendsTable ");
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
}
}
Don't use NSASCIIStringEncoding, especially with non-ASCII characters.
All strings used with SQLite must be UTF-8.
To use parameters, you actually must have parameter markers in the query:
"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES (?,?)"
(This automatically gets rid of formatting problems.)
You must check the return value of sqlite3_prepare_v2.
The two parameter numbers are 1 and 2, not 1 and 3.
You must call sqlite3_bind_text before calling sqlite3_step.
Replace cStringUsingEncoding:NSASCIIStringEncoding with UTF8String.
const char *sql =[stQuery UTF8String];
Nice Tutorial on database
You are taking %# for facebook id in below line and it is for String. and i hope, your facebook id field type in database is not integer.
NSString *stQuery=[NSString stringWithFormat:#"INSERT INTO NewFriendsTable (friend_id,friend_fullname) VALUES ('%#','%#')",[arrNewFriendsId objectAtIndex:i],[arrNewFriendsFullName objectAtIndex:i]];
You can try below if your facebook id field is not integer.
sqlite3_bind_text(selectstmt, 1,[NSString stringWithFormat:#"%#",[[arrNewFriendsId objectAtIndex:i] UTF8String]], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(selectstmt, 3, [NSString stringWithFormat:#"%#",[[arrNewFriendsFullName objectAtIndex:i] UTF8String]], -1, SQLITE_TRANSIENT);
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 :(
-(IBAction)btnAddToFavorite:(id)sender
{
[self insertDataToFavourites:[[tempDict objectForKey:#"pageid"]intValue]:[tempDict objectForKey:#"desc"]];
}
-(void)insertDataToFavourites:(int)pageid :(NSString *)description
{
sqlite3_stmt *insertStatement = nil;
NSString *sql;
int returnvalue;
sql = [NSString stringWithFormat:#"insert into AddFavorite (Id,Description) VALUES (?,?)"];
returnvalue = sqlite3_prepare_v2(database, [sql UTF8String], -1, &insertStatement, NULL);
NSLog(#"\nFavorite ID is:--> %d &\nDescription is:--> %#",[[tempDict valueForKey:#"pageid"] intValue] ,[tempDict valueForKey:#"desc"]);
if (returnvalue == 1){
NSAssert1 (0,#"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
sqlite3_bind_text(insertStatement, 1,[[tempDict objectForKey:#"pageid"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(insertStatement, 2,[[tempDict objectForKey:#"desc"] UTF8String], -1, SQLITE_TRANSIENT);
if (SQLITE_DONE != sqlite3_step(insertStatement)){
NSAssert1(0, #"Error while inserting data. '%s'", sqlite3_errmsg(database));
}
else{
sqlite3_reset(insertStatement);
}
sqlite3_finalize(insertStatement);
}
This is a code to insert data into database. It is showing data like this, when i check database using select query.
Can anybody tell me where is the mistake ?
Thanks.
try to use the pageid method variable and not your dict, maybe the dict is wrong. On the other hand you insert for id
sqlite3_bind_text(insertStatement, 1,[[tempDict objectForKey:#"pageid"] UTF8String], -1, SQLITE_TRANSIENT);
i think this might be not correct.
For test case you can insert in SQLiteManager as sqlstatement
insert into AddFavorite (Id,Description) VALUES (1,'TestDescription');
to check if the statement is correct.
I would use this code to insert your values, if id is an int in your database
-(void)insertDataToFavourites:(int)pageid :(NSString *)description
{
sqlite3 *database;
if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "INSERT INTO AddFavorite (Id,Description) VALUES (?,?);";
sqlite3_stmt *compiled_statement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiled_statement, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiled_statement, 1, pageid);
sqlite3_bind_text(compiled_statement, 2, [description UTF8String] , -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(compiled_statement) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %lld", sqlite3_last_insert_rowid(database));
}
sqlite3_finalize(compiled_statement);
}
sqlite3_close(database);
}
I'm getting error to store array value in database. My last array value is stored in database. I thought the counter was proper. Where in this code did I do wrong? I'm creating a counter in the database to store array values.
i got this error message Save Error: library routine called out of sequence
on this line ############
-(BOOL)Add
{
NSString *filePath = [BaseModal getDBPath];
NSLog(#"this check:%#",filePath);
sqlite3 *database;
if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "insert into Name(name,Date,CDate,Attach form,data) VALUES (?,?,?,?,?)";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
NSString* date = [dateFormat stringFromDate:Date];
NSString* cdate = [dateFormat stringFromDate:Cdate];
sqlite3_bind_text( compiledStatement, 1, [name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 2, [date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement, 3, [cdate UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_text( compiledStatement, 4, [UDate UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compiledStatement, 4, Attach form);
sqlite3_bind_text( compiledStatement, 5, [data UTF8String], -1, SQLITE_TRANSIENT);
//sqlite3_bind_int(compiledStatement, 6, i);
}
if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
NSLog( #"Save Error: %s", sqlite3_errmsg(database));
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
return FALSE;
}
else {
TaskId=sqlite3_last_insert_rowid(database);
sqlite3_reset(compiledStatement);
//sqlite3_finalize(compiledStatement);
//now insert into name table
const char *sqlStatement3 = "insert into TeaInfo(TitleId,TeaId) VALUES (?,?)";
sqlite3_stmt *compiledStatement3;
if(sqlite3_prepare_v2(database, sqlStatement3, -1, &compiledStatement3, NULL) == SQLITE_OK) {
sqlite3_bind_int(compiledStatement3, 1, TitleId);
for(int counter=0;counter<[self.Tea count];counter++)//***************************
{
Teaclass *teaInfo=(Teaclass*)[self.Teas objectAtIndex:counter];
sqlite3_bind_int( compiledStatement3, 2, teaInfo. TeaId);
}
if(sqlite3_step(compiledStatement3) != SQLITE_DONE )//#############
{
NSLog( #"Save Error: %s", sqlite3_errmsg(database) );
//return FALSE;
}
//}
}
return TRUE;
}
}
return TRUE;
}
Solved problem in if(sqlite3_prepare_v2(database, sqlStatement3, -1, &compiledStatement3, NULL) == SQLITE_OK)this line I have to write in for loop then row is inserted properly.
Try the code below,
const char *sqlStatement3 = "insert into TeaInfo(TitleId,TeaId) VALUES (\"VALUE1\",\"VALUE2\)";
i have a huge problem with my sqlite3 database on the iphone sdk 4. I can only write/read the database once! With the iphone sdk 3.2 everything had just worked fine.
After the first try i get the errormessage "SQLITE_BUSY".
Here's a snippet of my code i use:
-(void) addFavoriteOdv:(Odv*)odv name:(NSString*)name;
{
#synchronized(self)
{
if(name == nil)
name = odv.name;
NSString* rowId = [self addOdv:odv];
int existing = [self numberOfFavoriteOdvs:rowId];
if(existing == 0)
{
sqlite3 *database = [self getOpenDatabase];
const char *insertStatement = [#"insert into favoriteodvs(odv, name) values (?, ?)" UTF8String];
int retCode = 0;
sqlite3_stmt *statement;
retCode = sqlite3_prepare_v2(database, insertStatement, -1, &statement, NULL);
if(retCode == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [rowId UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [name UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(statement)!= SQLITE_DONE)
{
NSLog(#"SQL Error: %s", sqlite3_errmsg(database));
NSLog(#"Inserting FavoriteOdv failed");
}
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
}
Has anybody an idea? I use the libsqlite3.dylib.
Thanks,
Sebastian
to reuse your prepared statement - you must call
sqlite3_reset(statement);
right after you're received "SQLITE_DONE" from sqlite3_step. After that you can bind another data and run your query again.
p.s. wrong:
const char *insertStatement = [#"insert into favoriteodvs(odv, name) values (?, ?)" UTF8String];
correct:
const char *insertStatement = "insert into favoriteodvs(odv, name) values (?, ?)";