I am new to iOS sqlite. I am working on xcode 4.2. Here is my code:
.h file
#import <UIKit/UIKit.h>
#import </usr/include/sqlite3.h>
@interface RpTestViewController : UIViewController {
sqlite3 *db;
NSString *dbPath;
}
@end
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
dbPath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"test.db"]];
NSFileManager *fileMgr = [NSFileManager defaultManager];
if([fileMgr fileExistsAtPath: dbPath] == NO)
{
const char *_dbPath = [dbPath UTF8String];
if(sqlite3_open(_dbPath, &db) == SQLITE_OK)
{
char *err;
const char *sql = "create table if not exists test_group (groupID integer primary key autoincrement, name, name_arr)";
if(sqlite3_exec(db, sql, NULL, NULL, &err) != SQLITE_OK)
{
NSLog(@"failed to create table");
}
else
{
NSLog(@"ok...");
}
}
}
}
But, i got following error when I try to run:
Ld /Users/remoteprogrammer/Library/Developer/Xcode/DerivedData/fitest-bjghexnpsekbougtezdjshnnugzl/Build/Products/Debug-iphonesimulator/fitest.app/fitest normal i386
cd /MRK/iPhone/test/fitest
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/clang -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk -L/Users/remoteprogrammer/Library/Developer/Xcode/DerivedData/fitest-bjghexnpsekbougtezdjshnnugzl/Build/Products/Debug-iphonesimulator -F/Users/remoteprogrammer/Library/Developer/Xcode/DerivedData/fitest-bjghexnpsekbougtezdjshnnugzl/Build/Products/Debug-iphonesimulator -filelist /Users/remoteprogrammer/Library/Developer/Xcode/DerivedData/fitest-bjghexnpsekbougtezdjshnnugzl/Build/Intermediates/fitest.build/Debug-iphonesimulator/fitest.build/Objects-normal/i386/fitest.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -fobjc-arc -Xlinker -no_implicit_dylibs -D__IPHONE_OS_VERSION_MIN_REQUIRED=50000 -framework UIKit -framework Foundation -framework CoreGraphics -o /Users/remoteprogrammer/Library/Developer/Xcode/DerivedData/fitest-bjghexnpsekbougtezdjshnnugzl/Build/Products/Debug-iphonesimulator/fitest.app/fitest
Undefined symbols for architecture i386:
"_sqlite3_open", referenced from:
-[RpTestViewController viewDidLoad] in RpTestViewController.o
"_sqlite3_exec", referenced from:
-[RpTestViewController viewDidLoad] in RpTestViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How to resolve this problem.
All you have to add
libsqlite3.dyliblibrary to your project. And you can do it by going on Build Pahse in your Target Settings, then in "Link Binary With Libraries" you have to add the desired library and It will work.