简体   繁体   English

iPhone:SQLite数据库打开不起作用

[英]iPhone: sqlite db open is not working

I am new to iOS sqlite. 我是iOS sqlite的新手。 I am working on xcode 4.2. 我正在使用xcode 4.2。 Here is my code: 这是我的代码:

.h file .h文件

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

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

@end

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

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.dylib library to your project. 您只需libsqlite3.dylib库添加到您的项目中。 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. 您可以通过在“目标设置”中进行Build Pahse来完成此操作,然后在“将二进制文件与库链接”中添加所需的库,它将起作用。

Undefined symbols for architecture i386: 体系结构i386的未定义符号:

For this type of error you must check if you have added all the required framework or header files or not. 对于此类错误,您必须检查是否已添加所有必需的框架或头文件。

you must add libsqlite3.dylib to run your project. 您必须添加libsqlite3.dylib才能运行您的项目。

Thanks 谢谢

  • Select the bundle of you project (Project Name folder at the top of the Project navigator having blue icon) 选择您的项目包(Project导航器顶部的项目名称文件夹,带有蓝色图标)
  • Go to "Build Phases" 转到“构建阶段”
  • Expand "Link Binary with Libraries" 展开“将二进制文件与库链接”
  • Click on a (+) button at the left bottom side , which is used to add the library to the selected bundle project 单击左下角的(+)按钮,该按钮用于将库添加到选定的捆绑项目中
  • Find libsqlite3.0.dylib 查找libsqlite3.0.dylib
  • Click on the Add button 点击添加按钮

Now you Project should work properly 现在您的Project应该可以正常工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM