{
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];}
}
Asked
Active
Viewed 206 times
-1

StormeHawke
- 5,987
- 5
- 45
- 73

Muhammad Ali
- 3
- 6
-
Just a small heads up, if you plan to submit you app to the Apple App Store you need to Xcode 5.1.1 Apple no longer accepts any Apps build with older Xcode version. – rckoenes Oct 31 '14 at 12:43
-
I am trying this code for myself.if any better solution for this problem please help me – Muhammad Ali Oct 31 '14 at 12:48
-
1What does `errMsg` say when it fails? – rmaddy Oct 31 '14 at 14:40
1 Answers
0
Try This
-(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");
}
}
}
Update
This one works for me
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");
}
}
}

Prasanth S
- 3,725
- 9
- 43
- 75
-
Noe i am using your code,but the same answer "failed to open/create database" please help me – Muhammad Ali Oct 31 '14 at 12:42
-
1) Why alloc a new string for `databasePath`? 2) Log `errMsg` for any failures or call `sqlite3_errmsg`. – rmaddy Oct 31 '14 at 14:41