创建数据库代码
这段代码是正确的。我使用该代码创建了一个数据库,它成功运行并创建了一个数据库,但我测试了我的应用程序,所以我删除或删除了我的模拟器应用程序并再次运行我的应用程序,然后我看到没有创建数据库。
请告诉我我面临的实际问题是什么。
ClsUpdateNetworkViewController.h
#import <UIKit/UIKit.h>
#import "Person.h"
#import <sqlite3.h>
@interface ClsUpdateNetworkViewController : UIViewController{
UITextField *name;
UITextField *phone;
NSString *databasePath;
sqlite3 *contactDB;
int rowcount;
}
@property (nonatomic, strong) Person *person;
@property (nonatomic, strong) NSString *firstName;
@property (nonatomic, strong) NSString *lastName;
@property (nonatomic, strong) NSString *fullName;
@property (nonatomic, strong) NSString *phoneNumber;
@property (nonatomic, strong) NSString *workEmail;
@end
ClsUpdateNetworkViewController.m
#import "ClsUpdateNetworkViewController.h"
#import "ClsMainPageAppDelegate.h"
#import "ClsAddressBookViewController.h"
@interface ClsUpdateNetworkViewController ()
@end
@implementation ClsUpdateNetworkViewController
@synthesize person,firstName,lastName,fullName,phoneNumber,workEmail;
@synthesize name,phone;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self createDatabase];
}
-(void) createDatabase
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PHONE TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(contactDB);
}
else
{
NSLog(@"Failed to open/create database");
}
}
}
我测试或调试我的应用程序
Code is not going inside this condition.
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
// inside code is not executed.
}