{
NSString *docsDir;
NSArray *dirPaths;
dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
docsDir=[NSString stringWithFormat:@"%@",dirPaths[0]];
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"contacts.db"]];
NSFileManager *filemgr = [[NSFileManager defaultManager]fileExistsAtPath:databasePath];
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open (dbpath, &contactDB) == SQLITE_OK)
{
theStatus = @"asss";
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
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)
{
theStatus.text = @"database and table created";
}
else {
theStatus.text = @"Failed to open/create database";
}
}
}
[filemgr release];}
}
问问题
201 次
1 回答
0
尝试这个
-(void)databasecreation{
// 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];
//check the database is exist
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)" UTF8String];
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK){
NSLog(@"Failed to create tables");
}
sqlite3_close(contactDB);
} else {
NSLog( @"Failed to open/create database");
}
}
}
更新
这个对我有用
应用代理.h
#import <UIKit/UIKit.h>
#import "sqlite3.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
sqlite3 *contactDB;
NSString *databasePath;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,readonly) sqlite3 *contactDB;
@property (nonatomic,retain) NSString *databasePath;
@end
appdelegate.m
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize contactDB,databasePath;
NSString *docsDir;
NSArray *dirPaths;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self databasecreation];
return YES;
}
-(void)databasecreation{
// 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];
//check the database is exist
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)" UTF8String];
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK){
NSLog(@"Failed to create tables");
}
sqlite3_close(contactDB);
} else {
NSLog( @"Failed to open/create database");
}
}
}
于 2014-10-31T12:12:52.583 回答