iPhone:sqlite db open无效

时间:2014-03-14 07:47:53

标签: ios iphone sqlite xcode4.2

我是iOS sqlite的新手。我正在研究xcode 4.2。这是我的代码:

.h文件

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

@interface RpTestViewController : UIViewController {
    sqlite3 *db;
    NSString *dbPath;
}

@end

.m文件

- (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...");
            }
        }
    }
}

但是,当我尝试运行时出现以下错误:

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)

如何解决此问题。

3 个答案:

答案 0 :(得分:0)

您只需将libsqlite3.dylib库添加到项目中即可。 您可以通过在目标设置中进行Build Pahse,然后在&#34; Link Binary With Libraries&#34;中进行操作。你必须添加所需的库,它会工作。

答案 1 :(得分:0)

架构i386的未定义符号:

对于此类错误,您必须检查是否已添加所有必需的框架或头文件。

您必须添加libsqlite3.dylib才能运行您的项目。

由于

答案 2 :(得分:0)

  • 选择项目包(Project蓝色图标导航器顶部的Project Name文件夹)
  • 转到“构建阶段”
  • 展开“链接二进制文件库”
  • 点击左下方的(+)按钮,该按钮用于将库添加到选定的捆绑项目
  • 查找libsqlite3.0.dylib
  • 点击“添加”按钮

现在您的项目应该正常工作