Hi all,
I'm trying to use sqlite3 for db implementation but was confronted with 'SIGABRT' during runtime.
Database1ViewController.h:
Database1ViewController:
This is the error during runtime, main.m:
The error in my console:
Is it syntax error?
I'm trying to use sqlite3 for db implementation but was confronted with 'SIGABRT' during runtime.
Database1ViewController.h:
Code:
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface Database1ViewController : UIViewController
{
sqlite3 *db;
}
-(NSString *)filepath;
-(void) openDB;
-(void) createTableNamed:(NSString *)tableName
withField1:(NSString *)field1
withField2:(NSString *)field2;
-(void)insertRecordIntoTableNamed:(NSString *)tableName
withField1:(NSString *)field1
field1Value:(NSString *)field1Value
andField2:(NSString *)field2
field2Value:(NSString *)field2Value;
-(void)getAllRowsFromTableName: (NSString *)tableName;
@end
Database1ViewController:
Code:
#import "Database1ViewController.h"
@implementation Database1ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(NSString *)filepath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"database.sql"];
}
//function to opening db
-(void) openDB{
if (sqlite3_open([[self filepath] UTF8String], &db)!= SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Database failed to open.");
}
}
//function to creating table after opening db
-(void)createTableNamed:(NSString *)
tableName withField1:(NSString *)field1
withField2:(NSString *)field2{
char *err;
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@'('%@' ""TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Table failed to be created.");
}
}
//Function to insert record
-(void) insertRecordIntoTableNamed:(NSString *)tableName
withField1:(NSString *)field1 field1Value:(NSString *)field1Value
andField2:(NSString *)field2 field2Value:(NSString *)field2Value{
NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@'('%@', '%@')" "VALUES('%@', '%@')", tableName, field1, field2, field1Value, field2Value];
char *err;
if (sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Error updating database.");
}
}
- (void)viewDidLoad
{
[self openDB];
[self createTableNamed:@"Contact"
withField1:@"email"
withField2:@"name"];
//Merging values and functions together
for (int i=0; i<=2; i++) {
NSString *email = [[NSString alloc] initWithFormat:@"user%d@learn2develop.net", i];
NSString *name = [[NSString alloc] initWithFormat:@"user %d", i];
[self insertRecordIntoTableNamed:@"Contacts"
withField1:@"email" field1Value:email
andField2:@"name" field2Value:name];
[email release];
[name release];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
This is the error during runtime, main.m:
Code:
#import <UIKit/UIKit.h>
#import "Database1AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([Database1AppDelegate class]));[B][COLOR="Red"]Thread 1: Program received signal: "SIGABRT".[/COLOR][/B]
}
}
Code:
2012-03-28 10:38:04.577 Database1[332:f803] *** Assertion failure in -[Database1ViewController insertRecordIntoTableNamed:withField1:field1Value:andField2:field2Value:], .../Database1ViewController.m:65
2012-03-28 10:38:04.596 Database1[332:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error updating database.'
*** First throw call stack:
(0x1584052 0x1715d0a 0x152ca78 0xa5b2db 0x2ca7 0x2e48 0x19864e 0xf8a73 0xf8ce2 0xf8ea8 0xffd9a 0x251f 0xd09d6 0xd18a6 0xe0743 0xe11f8 0xd4aa9 0x146efa9 0x15581c5 0x14bd022 0x14bb90a 0x14badb4 0x14baccb 0xd12a7 0xd2a9b 0x2232 0x21a5)
terminate called throwing an exceptionsharedlibrary apply-load-rules all