简体   繁体   English

如何在目标C中更新SQLite数据库中的表?

[英]How to update a table in SQLite database in objective C?

I created a table named "dept" in sqlite manually that has username (as Varchar) and Password (as Integer). 我在sqlite中手动创建了一个名为“ dept”的表,该表具有用户名(如Varchar)和密码(如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 提前致谢

You can't not update fils in the mainBundle, these files are readonly. 您无法在mainBundle中更新fil,这些文件是只读的。

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 . database2databasePath2相同。 You are only using up memory here. 您只在这里用完内存。

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) 然后您可以在路径databasePath(文档目录)中工作

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 :) 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM