1

I am using sqlite and I try to insert some data to sqlite database.It is not work. I have a one table which name is LabUpdate but I have some errors like this:

2012-04-02 08:49:58.158 SqliteDeneme[361:207] Failed from sqlite3_prepare_v2. Error is:  no such column: deneme
2012-04-02 08:49:58.159 SqliteDeneme[361:207] Compiled Statement has error code:1:INSERT    INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate,    PackageNo, Status, Description)    VALUES(1,1,0.100000,0.100000,0.100000,0.100000,deneme,1,1,deneme)
2012-04-02 08:49:58.160 SqliteDeneme[361:207] ExecuteNonQuery has error
2012-04-02 08:49:58.160 SqliteDeneme[361:207] Failed from sqlite3_step. Error is:  library routine called out of sequence

My code like this:

- (IBAction)buttonClick:(id)sender {

NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"SqliteTestDb.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
    NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &cruddb) == SQLITE_OK))
{
    NSLog(@"An error has occured.");
}

if(sqlite3_open([dbPath UTF8String], &cruddb) ==SQLITE_OK){

int  str1 =1;
int str2 =1;
float str3 =0.1;
float str4 =0.1;
float str5 =0.1;
float str6 =0.1;
NSString *str7 =@"deneme";
int str8 =1;
int str9 =1;
NSString *str10=@"deneme";

NSString* SQL = [NSString stringWithFormat:@"INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES(%i,%i,%f,%f,%f,%f,%@,%i,%i,%@)",str1,str2,str3,str4,str5,str6,str7,str8,str9,str10];

stmt = [self prepare:SQL];

    sqlite3_step(stmt);
    sqlite3_finalize(stmt); 
    sqlite3_close(cruddb);

if (sqlite3_step(stmt) != SQLITE_DONE) 
{
    NSLog(@"ExecuteNonQuery has error");
    NSLog( @"Failed from sqlite3_step. Error is:  %s", sqlite3_errmsg(cruddb) );

}
else
{
    int rowsaffected = sqlite3_changes(cruddb);
    NSLog(@" rowsaffected %i",rowsaffected);
}

}
}

-(sqlite3_stmt*)prepare:(NSString*)query
{
sqlite3_stmt *queryHandle;


const char *sqlStatement = (const char *) [query UTF8String];

if(sqlite3_prepare_v2(cruddb, sqlStatement, -1, &queryHandle, NULL) != SQLITE_OK) 
{
    int error = sqlite3_prepare_v2(cruddb, sqlStatement, -1, &queryHandle, NULL);

    NSLog( @"Failed from sqlite3_prepare_v2. Error is:  %s", sqlite3_errmsg(cruddb) );

    NSLog(@"Compiled Statement has error code:%i:%@",error,query);
}

return queryHandle;
}

what is the problem? How can I solve this? Thanks for your reply.

3
  • 1
    Not that it really answers your question, but I thoroughly recommend using an Objective-C sqlite wrapper like FMDB or PLDatabase - it'll make your life so much easier. Commented Apr 2, 2012 at 6:32
  • can you post a NSLog(@"SQL: %@", SQL ) ? But you should quote mark your strings! e.g. : NSString* SQL = [NSString stringWithFormat:@"INSERT INTO LabUpdate (IsSuccess, ProducerId, Latitude, Longitude, Altitude, Slope, SampleDate, PackageNo, Status, Description) VALUES(%i,%i,%f,%f,%f,%f,'%@',%i,%i,'%@')",str1,str2,str3,str4,str5,str6,str7,str8,str9,str10]; Commented Apr 2, 2012 at 7:43
  • For some reason you are executing sqlite3_prepare_v2() and if that fails you are executing it again in order to get the error code. That is nonsense; do it in one step. Commented Apr 2, 2012 at 9:18

1 Answer 1

2

When you try to run an SQL statement, everything that is not a number needs to be quoted e.g.

INSERT INTO foo (column1, column2) VALUES ('bar' , 'baz');
                                        // ^^^^^   ^^^^^ string values quoted.

However, I strongly recommend using sqlite3_bind instead of formatting values directly into your SQL statement.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.