I created a table using SQLite Manager(firefox plugin) with 4 columns and 5 rows.
Now i wanted to fetch an entire random row from that table with help of following code but it doesnt works as i have never used it before.. what am i possibly doing wrong??
-(NSDictionary *)fetchQuestionsWithRowID:(NSInteger)rowID withDbPath:(NSString *)dbPath
{
NSDictionary *questDic=[NSDictionary dictionary];
sqlite3 *database=nil;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "SELECT * FROM quizQuestions Where rowid=?";
sqlite3_stmt *selectstmt;
int result = sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL);
if(result != SQLITE_OK) {
NSLog(@"Prepare-error #%i: %s", result, sqlite3_errmsg(database));
}
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
// confused here
sqlite3_bind_text16(selectstmt, 1, &rowID, -1, SQLITE_TRANSIENT);
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSString *str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
NSLog(@"string is %@",str);
}
}
}
sqlite3_close(database);
return questDic;
}
Hemant, look at below code may be it help but you need to make that code according to your need, means need to correct table, etc:
Also, have you import your db? And, one more thing before doing any operation you must go for checking that the following file or db exists or not.
Hope, it’ll sort-out your problem