sqlite3_open - '数据库无法打开'

Posted

技术标签:

【中文标题】sqlite3_open - \'数据库无法打开\'【英文标题】:sqlite3_open - 'Database Failed to open'sqlite3_open - '数据库无法打开' 【发布时间】:2014-06-06 08:21:49 【问题描述】:

我是 ios 编程新手。 我不知道我的错误的原因是什么。 我的代码只是在应用程序启动时加载数据库,但在我运行它时它会强制关闭。

这是我的头文件

//
//  ViewController.h
//  Manicuriousv2
//
//  Created by Bonafide on 6/5/14.
//  Copyright (c) 2014 Bonafide_iOSTeam. All rights reserved.
//

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

@interface ViewController : UIViewController <UITabBarDelegate, UITableViewDataSource>

    sqlite3 *AppDB;


- (IBAction)btnAvailPop:(id)sender;
- (IBAction)btnRstPop:(id)sender;
- (NSString *) filePath;
- (void) openDB;

- (void)
createTable: (NSString *) Ref_ServType
withfield1: (NSString *) ServID
withfield2: (NSString *) ServiceTitle;


@end

实施文件:

   //
//  ViewController.m
//  Manicuriousv2
//
//  Created by Bonafide on 6/5/14.
//  Copyright (c) 2014 Bonafide_iOSTeam. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)
createTable: (NSString *) Ref_ServType
withfield1: (NSString *) ServID
withfield2: (NSString *) ServiceTitle;





    -(NSString *) filePath
    NSArray *paths =NSSearchPathForDirectoriesInDomains
    (NSDocumentationDirectory,NSUserDomainMask, YES);
    return [[paths objectAtIndex:0]
    stringByAppendingPathComponent:@"ManicuriousDB.db"
            ];



-(void) openDB
    if (sqlite3_open([[self filePath] UTF8String], &AppDB) != SQLITE_OK)
        sqlite3_close(AppDB);
        NSAssert(0,@"Database Failed to open");
    else

        NSLog(@"Database Opened!");
    
    


- (void)viewDidLoad

    [self openDB];
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


- (IBAction)btnAvailPop:(id)sender 
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"Avail List"
    message:@"Are you sure you want to avail the services you have selected?"
    delegate:nil
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"Avail", nil];

    [alert show];




- (IBAction)btnRstPop:(id)sender 
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"Avail List"
    message:@"Are you sure you want to reset the list of services you have selected?"
    delegate:nil
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"Reset", nil];

    [alert show];


@end

错误信息:

2014-06-06 16:43:28.502 Manicuriousv2[2261:60b] Cannot find executable for CFBundle 0x99341f0 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle> (not loaded)
2014-06-06 16:43:28.590 Manicuriousv2[2261:60b] *** Assertion failure in -[ViewController openDB], /Users/jennifer/Desktop/Manicuriousv2/Manicuriousv2/ViewController.m:38
2014-06-06 16:43:28.593 Manicuriousv2[2261:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Database Failed to open'
*** First throw call stack:
(
    0   CoreFoundation                      0x018d61e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x016558e5 objc_exception_throw + 44
    2   CoreFoundation                      0x018d6048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x012354de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   Manicuriousv2                       0x00002293 -[ViewController openDB] + 339
    5   Manicuriousv2                       0x00002307 -[ViewController viewDidLoad] + 55
    6   UIKit                               0x0043433d -[UIViewController loadViewIfRequired] + 696
    7   UIKit                               0x004345d9 -[UIViewController view] + 35
    8   UIKit                               0x0045f509 -[UINavigationController rotatingSnapshotViewForWindow:] + 52
    9   UIKit                               0x007a6e3c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 420
    10  UIKit                               0x0035bc22 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 1495
    11  UIKit                               0x0035b646 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
    12  UIKit                               0x0035b518 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
    13  UIKit                               0x0035b5a0 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
    14  UIKit                               0x0035a63a __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
    15  UIKit                               0x0035a59c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
    16  UIKit                               0x0035b2f3 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
    17  UIKit                               0x0035e8e6 -[UIWindow setDelegate:] + 449
    18  UIKit                               0x00438b77 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
    19  UIKit                               0x00354474 -[UIWindow addRootViewControllerViewIfPossible] + 591
    20  UIKit                               0x003545ef -[UIWindow _setHidden:forced:] + 312
    21  UIKit                               0x0035486b -[UIWindow _orderFrontWithoutMakingKey] + 49
    22  UIKit                               0x0dcda587 -[UIWindowAccessibility(SafeCategory) _orderFrontWithoutMakingKey] + 77
    23  UIKit                               0x0035f3c8 -[UIWindow makeKeyAndVisible] + 65
    24  UIKit                               0x0030fbc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    25  UIKit                               0x00314667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    26  UIKit                               0x00328f92 -[UIApplication handleEvent:withNewEvent:] + 3517
    27  UIKit                               0x00329555 -[UIApplication sendEvent:] + 85
    28  UIKit                               0x00316250 _UIApplicationHandleEvent + 683
    29  GraphicsServices                    0x038cbf02 _PurpleEventCallback + 776
    30  GraphicsServices                    0x038cba0d PurpleEventCallback + 46
    31  CoreFoundation                      0x01851ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    32  CoreFoundation                      0x018519db __CFRunLoopDoSource1 + 523
    33  CoreFoundation                      0x0187c68c __CFRunLoopRun + 2156
    34  CoreFoundation                      0x0187b9d3 CFRunLoopRunSpecific + 467
    35  CoreFoundation                      0x0187b7eb CFRunLoopRunInMode + 123
    36  UIKit                               0x00313d9c -[UIApplication _run] + 840
    37  UIKit                               0x00315f9b UIApplicationMain + 1225
    38  Manicuriousv2                       0x0000296d main + 141
    39  libdyld.dylib                       0x01e1f701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

发布完整的错误信息。 我已经添加了错误信息。 【参考方案1】:

查看您发布的异常消息:

2014-06-06 16:43:28.590 Manicuriousv2[2261:60b] *** Assertion failure in -[ViewController openDB], /Users/jennifer/Desktop/Manicuriousv2/Manicuriousv2/ViewController.m:38
2014-06-06 16:43:28.593 Manicuriousv2[2261:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Database Failed to open'

* 首先抛出调用栈:

第一行告诉你问题出在哪里:

-[ViewController openDB]

第二行告诉你错误是什么:

'Database Failed to open'

所以问题在于你的这部分代码:

-(void) openDB
    if (sqlite3_open([[self filePath] UTF8String], &AppDB) != SQLITE_OK)
        sqlite3_close(AppDB);
        NSAssert(0,@"Database Failed to open");
    else

        NSLog(@"Database Opened!");
    

根据有根据的猜测,我认为问题出在您调用 sqlite3_open() 函数时。

sqlite3_open() 失败最可能的失败原因是因为您使用的路径是只读的。

回顾您的代码,您的 -filePath 方法正在使用 NSDocumentationDirectory,而它可能应该使用 NSDocumentDirectory

【讨论】:

谢谢,现在可以了。错误在于“NSDocumentDirectory”,就像你说它应该是“NSDocumentDirectory”一样。

以上是关于sqlite3_open - '数据库无法打开'的主要内容,如果未能解决你的问题,请参考以下文章

qsqlite设置了密码打开时无密码提示

sqlite学习笔记之sqlite3_open函数的使用

iOS的SQLite利用FMDB进行操作时无法打开数据库

如果 sqlite3_open() 返回错误和有效的数据库连接怎么办?

108.sqllite3(C语言数据库库)详解

iOS学习笔记16-SQLite应用