2

I created a table named "dept" in sqlite manually that has username (as Varchar) and Password (as Integer).

I used the following code to update a table

NSString *database2=[[NSBundle mainBundle]pathForResource:@"deptDatabase" ofType:@"sqlite"];
NSString *databasePath2=[NSString stringWithFormat:@"%@",database2];

const char *dbPath=[databasePath2 UTF8String];
if (sqlite3_open(dbPath, &dbHandler)==SQLITE_OK) {
    NSLog(@"database Opened");
    const char* updateQuery="update dept set password=1234 where username='suren'";
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(dbHandler, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
        NSLog(@"Query Executed");
    }
}

sqlite3_close(dbHandler);

but the table seems to be not updating. Can anyone pls tell me how to update the table by altering the above code. Thanks in advance

2 Answers 2

3

You can't not update fils in the mainBundle, these files are readonly.

To make changes to the database, you will have to copy it to the document directory. And use the database in the document directory and not the one in the main bundle. Only use the one in the main bundle as a payload file to be copied to the document directory if there is not file there.


Why do you create a new string here:

NSString *database2=[[NSBundle mainBundle]pathForResource:@"deptDatabase" ofType:@"sqlite"];
NSString *databasePath2=[NSString stringWithFormat:@"%@",database2];

The database2 is same as databasePath2. You are only using up memory here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks...As you told files in the mainBundle are read only, what is the alternative way to solve my issue.
I've edited my answer to suggest a way. Copy the file to the document directory and don't use the one the main bundle. Just use the the file in main bundle as a payload.
3

copy file from bundle to documents directory

NSString *databaseName = @"YOUR DB NAME WITH EXTENSION";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success=[fileManager fileExistsAtPath:databasePath];

if (!success) {

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];  
}

then u can work in the path databasePath (documents directory)

const char *dbPath=[databasePath UTF8String];
if (sqlite3_open(dbPath, &dbHandler)==SQLITE_OK) {
    NSLog(@"database Opened");
    const char* updateQuery="update dept set password=1234 where username='suren'";
    sqlite3_stmt *stmt;
    if (sqlite3_prepare_v2(dbHandler, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
        NSLog(@"Query Executed");
    }
}

sqlite3_close(dbHandler);

hope it helps.. happy coding :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.