当应用程序终止并再次启动时,无法插入 iPhone 的 sqlite 数据库

发布于 2024-11-07 23:14:05 字数 3058 浏览 0 评论 0原文

-(void)checkAndCreateDatabase{
    NSLog(@"hello");
    databaseName = @"EMAP234.sql";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    NSLog(@"database path %@",databasePath);
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

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

-(void)insertIntoDatabase:(NSString *)insertCommand{
    // Setup the database object
    sqlite3 *database;

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = [insertCommand UTF8String];
        sqlite3_stmt *compiledStatement;


        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            NSLog(@"inserted");
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {}
        }

        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

该应用程序第一次工作正常,但是当我重新启动它时,它可以读取较早的值,但无法插入新值...请帮助,

这就是我的调用方式...

DatabaseOperations *obj=[[DatabaseOperations alloc]init];
[obj checkAndCreateDatabase];
NSLog(@"patient ID %d",[obj readMaxPatientIDFromDatabase]);

patientID= [NSString stringWithFormat:@"HCI%d", ([obj readMaxPatientIDFromDatabase]+1)];
NSLog(@"patient ID %@",patientID);
[self generatePatientInsertCommand];
[obj insertIntoDatabase:patientInsertCommand];
[self generateCarrierInsertCommand];
[obj insertIntoDatabase:carrierInsertCommand];
[self generateSyncTableInsertCommand];
[obj insertIntoDatabase:syncTableInsertCommand];
NSString *msg=@"your PatientID is";
[obj release];
msg=[msg stringByAppendingString:patientID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REGISTERED" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
[alert release];
-(void)checkAndCreateDatabase{
    NSLog(@"hello");
    databaseName = @"EMAP234.sql";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    NSLog(@"database path %@",databasePath);
    BOOL success;

    // Create a FileManager object, we will use this to check the status
    // of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) return;

    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

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

-(void)insertIntoDatabase:(NSString *)insertCommand{
    // Setup the database object
    sqlite3 *database;

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = [insertCommand UTF8String];
        sqlite3_stmt *compiledStatement;


        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            NSLog(@"inserted");
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {}
        }

        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}

the app works fine for the first time but when i restart it , it can read the earlier values but cannot insert new ones ... pls help

here is how I am calling this....

DatabaseOperations *obj=[[DatabaseOperations alloc]init];
[obj checkAndCreateDatabase];
NSLog(@"patient ID %d",[obj readMaxPatientIDFromDatabase]);

patientID= [NSString stringWithFormat:@"HCI%d", ([obj readMaxPatientIDFromDatabase]+1)];
NSLog(@"patient ID %@",patientID);
[self generatePatientInsertCommand];
[obj insertIntoDatabase:patientInsertCommand];
[self generateCarrierInsertCommand];
[obj insertIntoDatabase:carrierInsertCommand];
[self generateSyncTableInsertCommand];
[obj insertIntoDatabase:syncTableInsertCommand];
NSString *msg=@"your PatientID is";
[obj release];
msg=[msg stringByAppendingString:patientID];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REGISTERED" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
[alert show];
[alert release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黎夕旧梦 2024-11-14 23:14:05

你好Ankit,我使用了你的代码并调用了

[self checkAndCreateDatabase];
[self insertIntoDatabase:[NSString stringWithFormat:@"INSERT INTO User (uId,userName,userBirthday) VALUES (\"%d\", \"%@\", \"%@\" )",3,@"Admin",@"2010-12-01"]];

我唯一改变的是使用一个不同的数据库文件,我在 checkAndCreateDatabase 函数中选择了该文件,

-(void)checkAndCreateDatabase{
    NSLog(@"hello");
    databaseName = @"MasterDB.sqlite";
....
}

代码对于每个具有新 uId 的插入都运行良好。请检查您的读取函数,如果可能,然后将读取函数的代码与上述代码以及调用这两个即读取和插入函数的方式粘贴在一起。

Hello Ankit I used your code and called

[self checkAndCreateDatabase];
[self insertIntoDatabase:[NSString stringWithFormat:@"INSERT INTO User (uId,userName,userBirthday) VALUES (\"%d\", \"%@\", \"%@\" )",3,@"Admin",@"2010-12-01"]];

The only thing I changed is using a different database file which I choose in checkAndCreateDatabase function by

-(void)checkAndCreateDatabase{
    NSLog(@"hello");
    databaseName = @"MasterDB.sqlite";
....
}

The code worked fine for every insert with a new uId. Please check your read Function and If Possible then paste the code for read function along with the above code and the way you call these two viz read and insert functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文