gpt4 book ai didi

ios - 如何访问 xcode 项目中的 sqlite 文件?

转载 作者:行者123 更新时间:2023-11-28 22:17:06 25 4
gpt4 key购买 nike

我是 IOS 开发的新手,我想知道如何访问 xcode 项目中的 sqlite 文件。在其他文件中,他们这样做:

NSString *path = [mainBundle pathForResource: @"image" ofType: @"png"]; 

但是当我尝试这行代码时,它在 sqlite 扩展中不起作用。

最佳答案

ios_sqlite_database

ABSQLite - 示例项目

- (void)viewDidLoad {
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, 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";
}
}

[filemgr release];
[super viewDidLoad];
}

sqlite-tutorial-for-ios-making-our-app

关于ios - 如何访问 xcode 项目中的 sqlite 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401761/

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