{
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