iPhone - Using sql database - insert statement failing
Posted
by
Satyam svv
on Stack Overflow
See other posts from Stack Overflow
or by Satyam svv
Published on 2010-12-29T17:13:55Z
Indexed on
2010/12/31
3:54 UTC
Read the original article
Hit count: 196
Hi, I'm using sqlite database in my iphone app. I've a table which has 3 integer columns. I'm using following code to write to that database table.
-(BOOL)insertTestResult {
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* dataBasePath = [documentsDirectory stringByAppendingPathComponent:@"test21.sqlite3"];
BOOL success = NO;
sqlite3* database = 0;
if(sqlite3_open([dataBasePath UTF8String], &database) == SQLITE_OK)
{
BOOL res = (insertResultStatement == nil) ? createStatement(insertResult, &insertResultStatement, database) : YES;
if(res)
{
int i = 1;
sqlite3_bind_int(insertResultStatement, 0, i);
sqlite3_bind_int(insertResultStatement, 1, i);
sqlite3_bind_int(insertResultStatement, 2, i);
int err = sqlite3_step(insertResultStatement);
if(SQLITE_ERROR == err)
{
NSAssert1(0, @"Error while inserting Result. '%s'", sqlite3_errmsg(database));
success = NO;
}
else
{
success = YES;
}
sqlite3_finalize(insertResultStatement);
insertResultStatement = nil;
}
}
sqlite3_close(database);
return success;}
The command sqlite3_step is always giving err as 19. I'm not able to understand where's the issue.
Tables are created using following queries:
CREATE TABLE [Patient] (PID integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,PFirstName text NOT NULL,PLastName text,PSex text NOT NULL,PDOB text NOT NULL,PEducation text NOT NULL,PHandedness text,PType text)
CREATE TABLE PatientResult(PID INTEGER,PFreeScore INTEGER NOT NULL,PForcedScore INTEGER NOT NULL,FOREIGN KEY (PID) REFERENCES Patient(PID))
I've only one entry in Patient table with PID = 1
BOOL createStatement(const char* query, sqlite3_stmt** stmt, sqlite3* database){
BOOL res = (sqlite3_prepare_v2(database, query, -1, stmt, NULL) == SQLITE_OK);
if(!res)
NSLog( @"Error while creating %s => '%s'", query, sqlite3_errmsg(database));
return res;}
© Stack Overflow or respective owner