pradeepj
pradeepj

Reputation: 516

Sqlite Connection

hello i am trying a sqlite database tutorial to build, but its not working can any one help me please:

Find my code below:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "PersonInfo.h"
#import "PersonDatabase.h"


@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSArray *personInfo=[[PersonDatabase database]getAllPersons];
    for (PersonInfo*info in personInfo) {
        NSLog(@"%d-%@",info.unique,info.nam);
    }// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end



#import <Foundation/Foundation.h>

@interface PersonInfo : NSObject{
    int unique;
    NSString * nam;
}
@property(nonatomic, assign)int unique;
@property(nonatomic, copy)NSString * nam;
-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name;
@end


#import "PersonInfo.h"

@implementation PersonInfo
@synthesize unique,nam;

-(id) initWithUniqueId:(int)uniqueId name:(NSString *)name{
    self=[super init];
    if (self) {
        self.unique=uniqueId;
        self.nam=name;
        NSLog(@"a");
    }
    return self;
}

@end



#import <Foundation/Foundation.h>
#import "sqlite3.h"


@interface PersonDatabase : NSObject{
    sqlite3 * database;

}
+(PersonDatabase *)database;
-(NSArray *)getAllPersons;
@end



#import "PersonDatabase.h"
#import "PersonInfo.h"

@implementation PersonDatabase
static PersonDatabase * database;

+(PersonDatabase *)database{
    if (database ==nil) {
        database =[[PersonDatabase alloc]init];

    }
    return database;
}
-(id)init{

    self=[super init];
    if (self) {
        NSString *sqliteDb=[[NSBundle mainBundle]pathForResource:@"admin" ofType:@"sqlite3"];
        if(sqlite3_open([sqliteDb UTF8String],&database)!=SQLITE_OK){
            NSLog(@"FAILED");
        }else {
            NSLog(@"passed");
        }
    }
    return self;
}
-(NSArray *)getAllPersons{
    NSMutableArray * returnArray=[[NSMutableArray alloc]init];
    NSString * query= @"SELECT * FROM names";
    NSLog(@"%@",query);
    sqlite3_stmt * statement;
    if(sqlite3_prepare_v2(database, [query UTF8String],-1, &statement,NULL)==SQLITE_OK){

        while (sqlite3_step(statement)==SQLITE_OK) {
            int uniqueId = sqlite3_column_int(statement, 0);
            char *nameChars=(char *)sqlite3_column_text(statement,1);
            NSString *name=[[NSString alloc]initWithUTF8String:nameChars];
            PersonInfo *info=[[PersonInfo alloc]initWithUniqueId:uniqueId name:name];
            [returnArray addObject:info];
        }
        sqlite3_finalize(statement);
    }
    else {
        NSLog(@"a");
    }
    return returnArray;
}

@end

i found the problem is that, it is not passing through the function below, but i don't know why?.. it also not giving any errors????

if(sqlite3_prepare_v2(database, [query UTF8String],-1, &statement,NULL)==SQLITE_OK)

Thanks in Advance!!

Upvotes: 1

Views: 191

Answers (1)

igoris
igoris

Reputation: 1476

sqlite3_open takes second parameter as sqlite3** but you pass PersonDatabase** (as well as sqlite3_prepare_v2 needs sqlite3*, not PersonDatabase*):

First of all, there is a lot of ambiguity in code:

Rename

static PersonDatabase *database;

to something like

static PersonDatabase *instance;

than

+(PersonDatabase *)database;

to

+(PersonDatabase *)sharedInstance;

So

+(PersonDatabase *)database{
    if (database ==nil) {
        database =[[PersonDatabase alloc]init];

    }
    return database;
}

Will be

+(PersonDatabase *)sharedInstance{
        if (instance ==nil) {
            instance =[[PersonDatabase alloc]init];

        }
        return instance;
    }

So now there is no ambiguity between sqlite3 *database and static PersonDatabase *database. And all should work.

Upvotes: 1

Related Questions