{
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. 以編程方式創建選項卡
- 4. 以編程方式創建Oracle數據庫和模式ADO.NET
- 5. 以編程方式在Spring中創建新的數據庫
- 6. 是否可以使用DBX以編程方式創建數據庫?
- 7. Xcode卡住編譯
- 8. 使用Microsoft.Web.Administration以編程方式創建
- 9. 如何在SQL Server中以編程方式創建數據庫?
- 10. 如何以編程方式創建Firebird數據庫?
- 11. 以C#編程方式從數據庫創建XSD
- 12. 以編程方式在iphone sdk中創建sqlite數據庫
- 13. 以編程方式創建SQL數據庫c#
- 14. 通過vb.net以編程方式創建Access數據庫
- 15. 以編程方式從生產數據庫創建空白數據庫
- 16. 在Asp.Net MVC應用程序中以編程方式創建新數據庫?
- 17. 以編程方式刷新數據庫?
- 18. 在新選項卡中以編程方式創建新的QTextEdit
- 19. 如何以編程方式使用Win32創建快捷方式
- 20. 以編程方式編輯SQLite數據庫中的數據
- 21. 如何以編程方式在influxdb中創建數據庫和用戶?
- 22. 以編程方式在xcode中創建和使用谷歌的kml文件iphone
- 23. Spark - 使用不同數據類型以編程方式創建模式
- 24. 如何在Xcode中以編程方式創建Uiview?
- 25. Xcode - 以編程方式創建UILabel並設置其位置
- 26. 以編程方式創建流程圖
- 27. 以編程方式重新創建現有的sql server數據庫?
- 28. 如何以編程方式確定Domino數據庫的創建者?
- 29. 以編程方式在TabControl中創建選項卡
如果您打算將應用程序提交給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