{
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];}
}
-1
A
回答
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");
}
}
}
更新
這一個對我的作品
appdelegate.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");
}
}
}
+0
Noe我使用你的代碼,但同樣的答案「無法打開/創建數據庫」請幫我 – 2014-10-31 12:42:36
+0
1)爲什麼要爲'databasePath'分配一個新的字符串? 2)記錄任何失敗的'errMsg'或者調用'sqlite3_errmsg'。 – rmaddy 2014-10-31 14:41:01
相關問題
- 1. 以編程方式創建數據庫
- 2. 如何使用Xcode以編程方式創建SQLite數據庫中的索引?
- 3. C#使用SMO以編程方式創建數據庫
- 4. 以編程方式創建選項卡
- 5. 以編程方式創建Oracle數據庫和模式ADO.NET
- 6. 在nlog中以編程方式創建數據庫以啓用使用DatabaseTarget
- 7. 如何以編程方式創建新的Mongo數據庫?
- 8. 以編程方式在Spring中創建新的數據庫
- 9. 是否可以使用DBX以編程方式創建數據庫?
- 10. Xcode卡住編譯
- 11. 使用Microsoft.Web.Administration以編程方式創建
- 12. 以編程方式在任意位置創建數據庫
- 13. 以C#編程方式從數據庫創建XSD
- 14. 如何以編程方式創建sqlite數據庫?
- 15. 以編程方式在SQL Server中創建數據庫
- 16. 如何在SQL Server中以編程方式創建數據庫?
- 17. 如何以編程方式在couchdb中創建數據庫?
- 18. 以編程方式創建數據庫(沒有權限?)
- 19. 以編程方式在iphone sdk中創建sqlite數據庫
- 20. Android - 如何以編程方式創建數據庫
- 21. 以編程方式創建SQL數據庫c#
- 22. 如何以編程方式創建Firebird數據庫?
- 23. 通過vb.net以編程方式創建Access數據庫
- 24. 以編程方式從生產數據庫創建空白數據庫
- 25. 如何使用C++在Sqlite中以編程方式創建數據庫?
- 26. 以編程方式使用客戶端庫創建Office 365組
- 27. 在Asp.Net MVC應用程序中以編程方式創建新數據庫?
- 28. 如何以編程方式知道Runtime.exec()進程何時卡住?
- 29. 以編程方式創建模式時獲取未知的數據庫異常?
- 30. 以編程方式刷新數據庫?
如果您打算將應用程序提交給Apple App Store,則需要Xcode 5.1.1 Apple不再接受任何使用舊版Xcode版本的Apps版本。 – rckoenes 2014-10-31 12:43:37
我正在嘗試這個代碼給我自己。如果這個問題的任何更好的解決方案,請幫助我 – 2014-10-31 12:48:46
當errMsg失敗時,它會說什麼? – rmaddy 2014-10-31 14:40:12