本文介绍了无法在iOS中打开/创建SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个文件DatabaseViewController.h文件,其中
I've 2 files DatabaseViewController.h file which has
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface DatabaseViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *status;
@property(strong,nonatomic) NSString *databasePath;
@property(nonatomic) sqlite3 *contactDb;
@end
另一个DatabaseViewController.m:
Another DatabaseViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
//Buid the path for database file
_databasePath = [[NSString alloc]
initWithString:[docsDir stringByAppendingPathComponent:@"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if([filemgr fileExistsAtPath: _databasePath ] == NO)
{
const char *db_path = [_databasePath UTF8String];
if(sqlite3_open(db_path, &_contactDb) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDb, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
_status.text = @"Failed to create table";
}
sqlite3_close(_contactDb);
} else {
_status.text = @"Failed to open/create database";
}
}
}
不幸的是,语句
if(sqlite3_open(db_path, &_contactDb) == SQLITE_OK)
永远不会成功。
它只是路由到
_status.text = @"Failed to open/create database";
有什么问题?
I确实将libsqlite3.dylib添加到项目中。我正在使用Xcode 4.5。
I did add libsqlite3.dylib to the project. I am using Xcode 4.5.
推荐答案
对 NSDocumentationDirectory
的引用应该是 NSDocumentDirectory
。
这篇关于无法在iOS中打开/创建SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!