Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:01:46+00:00 2026-05-25T17:01:46+00:00

After trying out FMDB and SQLiteManager4iOS and failing miserably to access the table, I

  • 0

After trying out FMDB and SQLiteManager4iOS and failing miserably to access the table, I have decided not to use wrappers and so created an sqlite database AnimalDatabase.sql (in Terminal) with a table called ‘animals’ having two fields id, name.

The table currently looks like this.

+----------+
|1|Elephant|
|2|Giraffe |
+----------+

I want to display this data on the simulator, so I create a simple ViewController, with a button and a label. On clicking the button, either Elephant or Giraffe gets displayed on the label.

Interface – DatabaseTestViewController.h

#import <UIKit/UIKit.h>
#import <sqlite3.h>

@interface DatabaseTestViewController : UIViewController {
    IBOutlet UILabel* result;
    NSString *databasePath;
    NSMutableArray *animals;
}

@property(nonatomic,retain) NSString *databasePath;
@property(nonatomic,retain) NSMutableArray *animals;
@property(nonatomic,retain) IBOutlet UILabel *result;

-(IBAction) findResult;

@end

Implementation – DatabaseTestViewController.m

#import "DatabaseTestViewController.h"

@implementation DatabaseTestViewController
@synthesize databasePath,result,animals;

-(IBAction)findResult {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    databasePath = [docDir stringByAppendingPathComponent:@"AnimalDatabase.sql"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath:databasePath];
    if(!success) {
        NSLog(@"File has to be copied");
        NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"AnimalDatabase.sql"];
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
        [fileManager release];
    }
    sqlite3 *database;
    animals = [[NSMutableArray alloc]init];
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
    {
        const char *sqlStatement = "select name from animals where id=2";
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL)==SQLITE_OK)
        {
            while(sqlite3_step(compiledStatement)==SQLITE_ROW)
            {
                NSLog(@"Name:%@",(char*)sqlite3_column_text(compiledStatement, 1));
                //[result setText:[NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement,1)]];
            }
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database);
}

- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
- (void)viewDidLoad {[super viewDidLoad];}
- (void)viewDidUnload {[super viewDidUnload];}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

In the NSLog, I get

Name: (null) 

Please help me out by explaining what mistake I have made. Also, I am using Xcode 4.1.1 on Lion. I added the AnimalDatabase.sql next to .h and .m files on the project tree.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:01:47+00:00Added an answer on May 25, 2026 at 5:01 pm

    I’m unclear whether this is the only issue, but this is certainly a problem:

    NSLog(@"Name:%@",(char*)sqlite3_column_text(compiledStatement, 1)); 
    

    You get the value of the column, and tell the compiler that it’s a char* (i.e., a C data type). But then you tell Objective C that it’s an object (%@).

    You can either convert the string into an NSString ([NSString stringWithCString:encoding:]) or tell NSLog that you’re using a null terminated string (%s).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After trying to format my JSON data by hand in javascript and failing miserably,
After trying everything I could, I finally created this test table: CREATE TABLE test_table
I'm not sure if this is even possible after trying to figure it out
After trying out VS2010b2 also my VS2008 installation changes the versions of solution and
My laptop is rally showing it's age, and last night after trying out the
After trying to figure out why a Capistrano task (which tried to start a
So i'm trying out MVC after only playing with it some time ago. Need
I'm trying out ASP.NET MVC, but, after reading a huge tutorial, I'm slightly confused.
I am new to backbone and trying out my first app after going through
after 2 days of trying to find out why my service isn't working I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.