iOS Programming with SQLite - Unable to read the table - Not using any wrapper
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开发者_StackOverflow中文版 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.
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
).
精彩评论