Ios 为什么数据库没有';没有创造?
我尝试在我的应用程序中使用sqlite3.0创建数据库,但数据库尚未创建。这是我的密码:Ios 为什么数据库没有';没有创造?,ios,objective-c,sqlite,nsfilemanager,Ios,Objective C,Sqlite,Nsfilemanager,我尝试在我的应用程序中使用sqlite3.0创建数据库,但数据库尚未创建。这是我的密码: // Getting the documents directory dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = di
// Getting the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Built the path to the database file
_databasePath = [[NSString alloc]
initWithString: [docsDir stringByAppendingPathComponent:
@"contacts1.db"]];//here the database name is contacts1.db.
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO)//checking Database exists
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
}
sqlite3_close(_contactDB);
}
}
我不知道是什么错误?我已经在ViewDidload()中实现了代码。请尝试像这样检查您的情况
NSArray *directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doctumentsDirectory = [directories lastObject];
self.databasePath = [[NSString alloc] initWithString:[doctumentsDirectory stringByAppendingPathComponent:@"/contacts1.db"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![filemgr fileExistsAtPath: _databasePath ])//checking Database exists
{
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"CREATE TABLE IF NOT EXISTS CONTACTS1 (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADDRESS TEXT, PHONE TEXT)";
if (sqlite3_exec(_contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
}
sqlite3_close(_contactDB);
}
}
什么是开放呼叫返回。什么是错误!返回时的sg?当您单步执行代码时会发生什么。这里缺少很多基本信息。您是否尝试设置断点并一步一步地跟踪它,以查看哪些行没有执行,哪些行可能为空?sqlite3_exec应该返回一个错误-是什么?它没有返回任何内容,我使用断点进行了检查,它不输入if条件语句block@user哪一块?太好了!这是你的努力创造出来的。谢谢各位