gpt4 book ai didi

iphone - 无法在 ios 中创建 sqlite3 数据库

转载 作者:行者123 更新时间:2023-11-29 13:09:27 24 4
gpt4 key购买 nike

创建数据库代码

这段代码是正确的。我使用该代码创建了一个数据库,它成功运行并创建了一个数据库,但我测试了我的应用程序,所以我删除或删除了我的模拟器应用程序并再次运行我的应用程序,然后我看到没有创建数据库。

请告诉我我面临的实际问题是什么。

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.
}

最佳答案

didFinishLaunchingWithOptions: 中调用此函数:

-(void)createdb
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"emp.sqlite"];

NSURL *dbURL = [NSURL fileURLWithPath:dbPath];

// copy a database from the bundle if not present
// on disk
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:dbPath])
{
NSURL *bundlePath = [[NSBundle mainBundle] URLForResource:@"emp" withExtension:@"sqlite"];
NSError *error = nil;
if (!bundlePath || ![fm copyItemAtURL:bundlePath toURL:dbURL error:&error]) {
NSLog(@"error copying database from bundle: %@", error);
}
}
else
{
NSLog(@"Success in Copy file");
}
}

关于iphone - 无法在 ios 中创建 sqlite3 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17741634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com