如何在应用程序启动时创建一个sqlite文件(didFinishLaunchingWithOptions)测试是否已经存在excist否则创建文件sqlite
答案 0 :(得分:2)
像这样...... sqlPath变量是你资源上预先制作的sql数据库的路径
- (void) checkAndCreateSQL
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[documentPath stringByAppendingString:@"/database.sql"]]) {
[[NSFileManager defaultManager] createFileAtPath:[documentPath stringByAppendingString:@"/database.sql"]
contents:[NSData dataWithContentsOfFile:sqlPath]
attributes:nil];
}
}
编辑1:
您可以使用以下命令行在Mac上创建数据库:
sqlite3 database.sql < DATABASE_CREATION.txt
在DATABASE_CREATION.txt中,如下所示:
CREATE TABLE IF NOT EXISTS `group` (
`id` integer PRIMARY KEY,
`name` text,
`position` integer
);
然后将database.sql文件直接放入项目资源中。 (如图像)
答案 1 :(得分:1)
您可能希望使用默认的Core Data库,而不是手动创建和处理单个sqlite文件。请查看官方Apple Core Data Programming Guide。它将自动处理应用程序中内部数据库的创建和更新。
答案 2 :(得分:0)
sqlite3 *reference2Database() {
if (_database == nil) {
// First, test for existence.
NSError *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"my.sqlite"];
if ([fileManager fileExistsAtPath:writableDBPath] == NO) {
// Database file doesnt exists. Copy the database at writable path (documents directory).
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"my.sqlite"];
[fileManager removeItemAtPath:writableDBPath error:nil];
BOOL databaseCopied = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!databaseCopied) {
// Handle the error...
}
}else {
// Open the database. The database was prepared outside the application.
if (sqlite3_open([writableDBPath UTF8String], &_database) != SQLITE_OK) {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(_database);
_database = nil;
// Additional error handling, as appropriate...
}
}
}
return _database;
}
//示例用法。
-(void) someDatabaseFunction {
sqlite3 *database = reference2Database();
// Do something with "database"...
}
//关闭数据库。应用程序终止时应该调用它。
void closeDatabase() {
if (_database == nil) return;
// Close the database.
if (sqlite3_close(_database) != SQLITE_OK) {
// Handle the error...
}
_database = nil;
}
注意:在文件的顶部,你应该有:static sqlite3 * _database = nil;
答案 3 :(得分:0)
我使用Matteo Bertozzi's SQLite Wrapper
使用以下代码创建我的sqlite数据库:
-(void)checkDatabase
{
if([[NSFileManager defaultManager] fileExistsAtPath:DBPATH] == NO)
{
sqlite = [[Sqlite alloc] init];
if (![sqlite open:DBPATH]) return;
[sqlite executeNonQuery:@"DROP TABLE yourtable"];
[sqlite executeNonQuery:@"CREATE TABLE yourtable (record1 TEXT NOT NULL,
record2 TEXT NOT NULL,
record3 TEXT NOT NULL,
record4 TEXT NOT NULL);"];
NSArray *results = [sqlite executeQuery:@"SELECT * FROM yourtable;"];
for (NSDictionary *dictionary in results) {
for (NSString *key in [dictionary keyEnumerator])
NSLog(@" - %@ %@", key, [dictionary objectForKey:key]);
}
[results release];
[sqlite release];
}
}