Upvote:18
Aprove answer
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath
{
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
}
Upvote:4
-(void)getdatabase
{
BOOL success;
NSFileManager *filemanager = [NSFileManager defaultManager];
NSError *error;
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DBPath = [pathArray objectAtIndex:0];
NSString *writableDBPath = @"";
writableDBPath = [DBPath stringByAppendingPathComponent:@"xyz.sqlite"];
NSLog(@"writableDBPath:%@",writableDBPath);
success = [filemanager fileExistsAtPath:writableDBPath];
if (!success) {
NSString *defaultDBpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"xyz.sqlite"];
success = [filemanager copyItemAtPath:defaultDBpath toPath:writableDBPath error:&error];
if (!success) {
NSAssert(0, @"failed to copy database at path with message '%@'.",[error localizedDescription]);
}
}
NSLog(@"111writableDBPath:%@",writableDBPath);
}
More Answer related to the Same Query
- HOW TO COPY SQLITE DATABASE WHEN APPLICATION IS LAUNCHED IN IOS? IN IOS
- LOCATION SERVICES DON'T STOP WHEN APPLICATION IS TERMINATED IN IOS
- SELECT STATEMENT IN SQLITE RECOGNIZING ROW NUMBER IN IOS
- HOW TO LOAD PHOTOS FROM PHOTO GALLERY AND STORE IT INTO APPLICATION PROJECT? IN IOS
- HIDE LARGE TITLE WHEN SCROLLING UP IN IOS
- ISSUE WHEN SUBMITTING APP IN APP STORE IN IOS
- WHY IS 'DIDSET' CALLED ON A PROPERTY WHEN I SET THE PROPERTY OF THAT PROPERTY? IN IOS
Upvote:2
func copyDatabaseIfNeeded(sourcePath : String) -> Bool {
var destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
destPath = destPath + "/foo.db3"
let databaseExistsWhereNeeded = FileManager.default.fileExists(atPath: destPath)
if (!databaseExistsWhereNeeded) {
do {
try FileManager.default.copyItem(atPath: sourcePath, toPath: destPath)
print("db copied")
}
catch {
print("error during file copy: \(error)")
}
}
return true
}
Upvote:0
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDocDirectory = [docPath objectAtIndex:0];
self.strDatabasePath = [strDocDirectory stringByAppendingPathComponent:YOUR_DB];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: self.strDatabasePath ] == NO)
{
const char *dbpath = [self.strDatabasePath UTF8String];
sqlite3 *contactDB;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS YOUR_Table (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
NSLog(@"Failed to create table");
}
sqlite3_close(contactDB);
More Answer related to the Same Query
- DEVICE TOKEN NOT RECEIVED WHEN REGISTERING FOR REMOTE NOTIFICATIONS IN SWIFT IN IOS
- ADD GIT COMMIT SHA TO IOS APPLICATION IN IOS
- FLUTTER STRIPE THROWS STRIPEEXCEPTION WHEN PRESENTING PAYMENT SHEET IN IOS
- PASS DATA WHEN DISMISS MODAL VIEWCONTROLLER IN SWIFT IN IOS
- UIBUTTON NOT SHOWING HIGHLIGHT WHEN TOUCHED IN IOS
- IOS7 ERROR WHEN SUBMITTING AN UPDATE. THIS BUNDLE DOES NOT SUPPORT ONE OR MORE DEVICES IN IOS
- HOW TO DETECT WHEN UINAVIGATIONCONTROLLER ANIMATION HAS FINISHED? IN IOS
- HOW DO I ACCESS MY APPLICATION DELEGATE'S WINDOW ACCESSOR METHOD FROM ANOTHER OBJECT? IN IOS
- SWIFT IOS -AVPLAYER VIDEO FREEZES / PAUSES WHEN APP COMES BACK FROM BACKGROUND IN IOS
- SWIFTUI: HOW TO DETECT WHEN LIST IS SCROLLED? IN IOS
Upvote:0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self copyDatabaseIfNeeded];
}
- (void) copyDatabaseIfNeeded
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sampleDB.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
/********* Database Path *********/
- (NSString *) getDBPath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"sampleDB.sqlite3"];
}
Upvote:0
sqlite3 *database;
NSString *databasePath;
Credit Goes to: stackoverflow.com
Related question with same questions but different answers
- UITEXTFIELD START TEXT FROM TOP WHEN HEIGHT IS INCREASED IN IOS
- SHOWING THE SEARCHBAR ONLY WHEN USER PULL DOWN THE TABLE IN IOS
- HOW TO TEST AN IOS APPLICATION ON THE CARPLAY SIMULATOR IN IOS
- SET BACKGROUND COLOR ON UIBUTTON WHEN DISABLED? IN IOS
- UIBUTTON TITLELABEL NOT VERTICALLY CENTERED WHEN USING MINIMUMSCALEFACTOR IN IOS
- IOS GOOGLE PLACES API: "THIS IP, SITE OR MOBILE APPLICATION IS NOT AUTHORIZED TO USE THIS API KEY" IN IOS
- "PROJECT.APP" NEEDS -ONONE SWIFT OPTIMIZATION LEVEL TO USE PREVIEWS IN IOS
- REACT NATIVE DUPLICATEERROR: DUPLICATED FILES OR MOCKS IN IOS
- UILABEL AND FONTS IN IOS
- CHECK IOS DEPLOYMENT TARGET OF A STATIC LIBRARY IN IOS
- UITEXTFIELD LIMIT THE NUMBER AND TYPE OF CHARACTERS IN IOS
- GLKIT'S GLKBASEEFFECT AND CUSTOM SHADERS IN IOS
- IOS - HOW CAN WE OFFER DISCOUNT THROUGH PROMO CODE FOR IN-APP PURCHASE? IN IOS
- HOW TO CHANGE TRACK POSITION ON LOCK SCREEN/CONTROL CENTER? IN IOS
- SYMBOLICATECRASH FROM XCODE 6 GM IN IOS
- SWITCH IN SWIFT - CASE LABEL IN A SWITCH SHOULD HAVE AT LEAST ONE EXECUTABLE STATEMENT IN IOS
- HOW TO SET BACKGROUND COLOR FOR UIPAGEVIEWCONTROLLER? IN IOS
- DETERMINE IF VIEW THAT APPEARS WAS PUSHED OR CAME FROM BACK BUTTON IN NAVIGATION BAR IN IOS
- HOW TO MAKE A BULLET LIST WITH SWIFT? IN IOS
- HOW TO CHANGE COLOR OF DETAIL DISCLOSURE BUTTON FOR TABLE CELL IN IOS
- HOW TO USE OPENCV 4 IN NATIVE C++ OF FLUTTER (IN 2022) (SUPPORT FLUTTER 2.0/3.0)? IN IOS
- HOW TO DISABLE JUST VERTICAL SCROLLING IN A UISCROLLVIEW? IN IOS
- RATE MY APPLICATION IN ITUNES WITHIN MY APPLICATION IN IPHONE IN IOS
- WHERE ARE THE IOS FRAMEWORKS BINARIES LOCATED IN THE FILESYSTEM? IN IOS
- CHANGING HEIGHT OF UIVIEW IN RESPONSE TO FINGER DRAG IN IOS
- TESTING FOR UIAPPLICATIONOPENSETTINGSURLSTRING EXISTENCE WITH SWIFT IN IOS
- XCODE DYNAMIC LIVE UPDATE LINE CHART IN IOS
- SWIFT: DOES NOT CONFORM TO PROTOCOL NSCODING IN IOS
- HOW TO GENERATE UNIQUE IDENTIFIER WHICH SHOULD WORK IN ALL IOS VERSIONS? IN IOS
- UITABLEVIEW SECTION HEADER ON RIGHT INSTEAD OF DEFAULT ON LEFT IN IOS
- OPEN VIEW CONTROLLER PROGRAMMATICALLY AND NOT USING A SEQUE IN IOS
- HOW TO KEEP NATIVE UI AFTER ACCEPT A CALL USING CALLKIT IN IOS
- ADD UIIMAGE ON TOP OF ANOTHER UIIMAGE IN IOS
- FLUTTER HTTPS HANDSHAKE ERROR IN CLIENT (OS ERROR: CERTIFICATE_VERIFY_FAILED: OK(HANDSHAKE.CC:363)) IN IOS
- DOES AFNETWORKING 2.0 SUPPORT IOS 6.0? IN IOS
- IOS 8.3 - UITABLEVIEW CELL NOT ALIGNED, INDENTATION TO THE LEFT IN IOS