virat virat - 1 month ago 7
Objective-C Question

Get only First value from sqlite database all the time when i fetch data

#import "DBManager.h"

static DBManager *sharedInstance = nil; //Class Object
static sqlite3 *database = nil; // Sqlite3 database object
static sqlite3_stmt *statement = nil; // sql query object

@implementation DBManager

if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init]; //memory allocation
[sharedInstance createDB];
return sharedInstance;

This is how i create database



NSFileManager *nsFileManager = [NSFileManager defaultManager];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSLog(@"document directory: %@",array);

NSString *documentPath = [array objectAtIndex:0];

NSLog(@"documentPath: %@",documentPath);

databasePath = [documentPath stringByAppendingString:@"/Database"];

if (![nsFileManager fileExistsAtPath:databasePath]) {
[nsFileManager createDirectoryAtPath:databasePath withIntermediateDirectories:NO attributes:nil error:nil];

NSLog(@"database path : %@",databasePath);

//NSLog(@"imageData : %@",imageData);
databasePath = [databasePath stringByAppendingString:@"/database.db"];
NSLog(@"databaseFilePath : %@",databasePath);
BOOL isSuccess = YES;

if (![nsFileManager fileExistsAtPath:databasePath]) {
const char *dbpath = [databasePath UTF8String];

NSLog(@"dbpath : %s",dbpath);

if (sqlite3_open(dbpath, &database) == SQLITE_OK) {

NSLog(@"database creation successfull");
char *errMsg;
const char *sql_stmt = "create table if not exists buddyMessage (message text)";

NSLog(@"sql is %s",sql_stmt);

if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)

isSuccess = NO;
NSLog(@"Failed to create table : %s",errMsg);

NSLog(@"table successful creation");

return isSuccess;
return isSuccess;


This is how i insert data to database

-(void)insertText:(NSString *)messageText{
NSLog(@"insertBuddyMessages : %@",databasePath);

const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK){
NSLog(@"database open");
NSString *insertSQL = [NSString stringWithFormat:@"insert into buddyMessage (message) values (\"%@\")",messageText];
const char *insert_stmt = [insertSQL UTF8String];

NSLog(@"insert_stmt : %s",insert_stmt);
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
NSLog(@"inserted ");
else {


Here i am using an Mutable array resultarray in which i adding names but when i fetch data using the following method i get the first element from database everytime. When i NSLog resultarray everytime it shows the firstvalue from database.

- (NSArray*) findmessage:(NSString*)message;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
NSString *querySQL = [NSString stringWithFormat:
@"select message from buddyMessage"];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
if (sqlite3_step(statement) == SQLITE_ROW)
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:name];
NSLog(@"array is %@",resultArray);
NSLog(@"array count %lu",(unsigned long)[resultArray count]);
NSString *last = [resultArray lastObject];
NSLog(@"last is %@ ",last);

// [[NSUserDefaults standardUserDefaults] setObject:last forKey:@"preferenceName"];
// [[NSUserDefaults standardUserDefaults] synchronize];

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// saving an NSString
[prefs setObject:last forKey:@"keyToLookupString"];

NSLog(@"prefs is %@", prefs);

NSLog(@"Not found");
// return nil;
return nil;



You have to call sqlite3_step in a loop and fill your array on each iteration.

while (sqlite3_step(statement) == SQLITE_ROW) {
    // store value here