Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't select rows after inserting into database
    primarykey
    data
    text
    <p>i've got a Problem when i try to get data from the Database after inserting some data. I can't select any row from the database.</p> <p>There are entries in the database but i can not get any result.</p> <p>I comment out this line in AppDelegate::application:didFinishWithOptions</p> <pre><code>[dbAccess importWithDelegate:result]; </code></pre> <p>and everything works just fine. But i can't find a solution to this problem. I hope you can help me.</p> <p>DatabaseAccess-Class. </p> <pre><code>#import "KejithDatabaseAccess.h" #import "KejithEntryQueryDelegate.h" @interface KejithDatabaseAccess (){ sqlite3 *db; NSString *writableDatabase; sqlite3_stmt *statement; } @end @implementation KejithDatabaseAccess -(id)init { if((self = [super init])) { // initialize database and store in _db } return self; } -(void)initializeDatabase { [self createEditableDatabase]; // open the database connection if(sqlite3_open([writableDatabase UTF8String], &amp;db) == SQLITE_OK){ NSLog(@"Database: Connection was opened successfully"); } else { // if something went wrong clean everything up sqlite3_close(db); NSAssert1(0, @"Database: Failed to open database connection. Error: '%s'", sqlite3_errmsg(db)); } } -(void)closeDatabase { if(sqlite3_close(db) != SQLITE_OK){ NSAssert1(0, @"Database: Failed to close database connection. Error: '%s'", sqlite3_errmsg(db)); } } -(void)createEditableDatabase { BOOL success; NSError *error; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [paths objectAtIndex:0]; // create writable database and store path for later use writableDatabase = [documentsDir stringByAppendingPathComponent:@"main-rw.db"]; success = [fileManager fileExistsAtPath: writableDatabase]; // if writable database already exists return if(success) return; // the editable database does not exist // copy the default DB to the application // documents directory NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"main.db"]; success = [fileManager copyItemAtPath:defaultPath toPath:writableDatabase error:&amp;error]; if(!success){ NSAssert1(0, @"Database: Failed to create writable database file: '%@'.", [error localizedDescription]); } } -(NSMutableArray *)queryWithDelegate { [self initializeDatabase]; // do we have an delegate? if(![self delegate]) return nil; NSMutableArray *result = [[self delegate] query:db]; [self closeDatabase]; return result; } -(void)importWithDelegate:(NSMutableArray *)collection { [self initializeDatabase]; [[self delegate] import:collection into:db]; sqlite3_finalize(statement); [self closeDatabase]; } -(sqlite3 *)getWritableDatabase { return db; } @end </code></pre> <p>Database Delegate-Class</p> <pre><code>#import "KejithEntryQueryDelegate.h" #import "KejithEntry.h" @interface KejithEntryQueryDelegate () @property sqlite3 *database; @end @implementation KejithEntryQueryDelegate @synthesize sql; @synthesize statement; -(id)init { if((self = [super init])){ [self initSQL]; } return self; } -(void)initSQL { sql = "SELECT _id, entry_title, entry_description, entry_phone, entry_fax, entry_email, entry_website FROM entry"; } -(NSMutableArray *)query:(sqlite3 *)database { // store database [self setDatabase:database]; // initialize array to store found objects NSMutableArray *entries = [[NSMutableArray alloc] init]; // prepare sql statement int sqlResult = sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL); if(sqlResult == SQLITE_OK){ while(sqlite3_step(statement) == SQLITE_ROW){ // allocate object to store row KejithEntry *entry = [[KejithEntry alloc] init]; // get data from columns NSMutableString *title = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)]]; NSMutableString *description = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 2)]]; NSMutableString *phone = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 3)]]; NSMutableString *fax = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 4)]]; NSMutableString *email = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 5)]]; NSMutableString *website = [NSMutableString stringWithString: [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 5)]]; // set data in object [entry setId:[NSNumber numberWithInt: sqlite3_column_int(statement, 0)]]; [entry setTitle:title]; [entry setDescription:description]; [entry setPhone:phone]; [entry setFax:fax]; [entry setEmail:email]; [entry setWebsite:website]; // put object into array [entries addObject:entry]; } // finalize the statement to release its resources sqlite3_finalize(statement); } else { // log errors NSLog(@"Database: Problem Occured in KejithEntryQueryDelegate.m"); NSLog(@"Database: Result Code: %d", sqlResult); NSLog(@"Database: SQL-Error: %s", sqlite3_errmsg(database)); } return entries; } -(void)import:(NSMutableArray *)collection into:(sqlite3 *)database { if([collection count] == 0) return; for(KejithEntry *entry in collection){ [self importEntry:entry into:database]; } } -(void)importEntry:(KejithEntry *)entry into:(sqlite3 *)database { sql = "INSERT INTO entry (entry_id, entry_title, entry_description, entry_phone, entry_fax, entry_email, entry_website, enty_latitude, entry_longitude, entry_category_id) \ VALUES \ (0,?,?,?,?,?,?,0,0,0);"; int sqlResult = sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL); sqlite3_bind_text(statement, 1, [[entry title] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 2, [[entry getDescription] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 3, [[entry phone] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 4, [[entry fax] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 5, [[entry email] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 6, [[entry website] UTF8String], -1, SQLITE_STATIC); if(sqlite3_step(statement) != SQLITE_DONE){ NSLog(@"&gt;&gt; Database: Failed to insert into Database"); NSLog(@"SQL Error Message: %s", sqlite3_errmsg(database)); } sqlite3_finalize(statement); } @end </code></pre> <p>AppDelegate::application:didFinishWithLaunchingOptions</p> <pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; // create delegate for entry xml parsing id delegate = [[KejithEntryXmlDelegate alloc] init]; KejithXmlParser *parser = [[KejithXmlParser alloc] initWithUrl:[[NSURL alloc] initWithString:entryXmlUrl]]; KejithDatabaseAccess *dbAccess = [[KejithDatabaseAccess alloc] init]; KejithEntryQueryDelegate *dbEntryDelegate = [[KejithEntryQueryDelegate alloc] init]; // set delegate to parse xml file [parser setDelegate:delegate]; // set delegate to query data to/from database [dbAccess setDelegate:dbEntryDelegate]; // get results of xml parsing NSMutableArray *result = [parser parse]; NSLog(@"Count of ParseResult: %d", [result count]); [dbAccess importWithDelegate:result]; NSLog(@"Count of DatabaseResult: %d", [[dbAccess queryWithDelegate] count]); return YES; } </code></pre> <p>Console-Output without [dbAccess importWithDelegate:result] commented out:</p> <pre><code>2013-12-08 15:59:44.035 staedteApp[30202:70b] Database: Connection was opened successfully 2013-12-08 15:59:44.048 staedteApp[30202:70b] Database: Connection was opened successfully 2013-12-08 15:59:44.049 staedteApp[30202:70b] Count of DatabaseResult: 0 </code></pre> <p>Console-Output with [dbAccess importWithDelegate:result] commented out:</p> <pre><code>2013-12-08 16:17:18.084 staedteApp[30267:70b] Database: Connection was opened successfully 2013-12-08 16:17:18.091 staedteApp[30267:70b] Count of DatabaseResult: 50 </code></pre> <p><strong>EDIT #1 -----</strong> Updated KejithEntryQueryDelegate::importEntry:into</p> <pre><code>-(void)importEntry:(KejithEntry *)entry into:(sqlite3 *)database { sql = "INSERT INTO entry (entry_id, entry_title, entry_description, entry_phone, entry_fax, entry_email, entry_website, enty_latitude, entry_longitude, entry_category_id) \ VALUES \ (0,?,?,?,?,?,?,0,0,0);"; int sqlResult = sqlite3_prepare_v2(database, sql, -1, &amp;statement, NULL); if(sqlResult != SQLITE_DONE){ sqlite3_bind_text(statement, 1, [[entry title] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 2, [[entry getDescription] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 3, [[entry phone] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 4, [[entry fax] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 5, [[entry email] UTF8String], -1, SQLITE_STATIC); sqlite3_bind_text(statement, 6, [[entry website] UTF8String], -1, SQLITE_STATIC); int sqlStepResult; if((sqlStepResult = sqlite3_step(statement)) != SQLITE_DONE){ NSLog(@"&gt;&gt; Database: Failed to insert into Database"); NSLog(@"SQL Error Message: %s", sqlite3_errmsg(database)); NSLog(@"SQL Step Result: %d", sqlStepResult); } } else { NSLog(@"Database: Problem Occured in KejithEntryQueryDelegate.m step"); NSLog(@"Database: Result Code: %d", sqlResult); NSLog(@"Database: SQL-Error: %s", sqlite3_errmsg(database)); } sqlite3_finalize(statement); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload