开发者

Not able to insert data in to sqlite table

开发者 https://www.devze.com 2023-02-25 17:24 出处:网络
I am new to iphone development,i was trying to insert the data in a sqlite table .but data is not inserted in the table.there was no error on the console.

I am new to iphone development,i was trying to insert the data in a sqlite table .but data is not inserted in the table.there was no error on the console.

NSString *string1=@"Test"; NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TestData" ofType:@"sqlite"];

if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK)
{
    sqlite3_stmt *compiledStatement=nil;
    char * sql = "insert into Test (title,summary,question,choice,answer) values (?,?,?,?,?)";

if (sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK)

{

        {

            sqlite3_bind_text(compiledStatement, 1, [string1 UTF8String], -1,SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 2,[string1 UTF8String] , -1,SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 3, [string1 UTF8String], -1,SQLITE_TRANSIENT);
        开发者_如何学编程    sqlite3_bind_text(compiledStatement, 4, [string1 UTF8String], -1,SQLITE_TRANSIENT);
            sqlite3_bind_text(compiledStatement, 5, [string1 UTF8String], -1,SQLITE_TRANSIENT);


            if(sqlite3_step(compiledStatement) != SQLITE_DONE )
            {
                NSLog( @"Error: %s", sqlite3_errmsg(database) );
            }
            else {
                NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database));
            }

        }


As your database in resource bundle you are trying to modify and update it. The better approach would be place your database first time in documents directory of sandbox and then perform operation on that database.

Here is the method using which you can move your database to documents directory of your application's sandbox. This method should be called only once while using database operation.(because only first time we need to place it at that location, other times we just need to access it).

Code :

// Name         :   configureDatabase:
// Description  :   Method configures the database with new name.
// Arguements   :   NSString    : Databse file name
// Retrun       :   None
-(void) configureDatabase:(NSString *)newDatabaseName
{
    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [[documentsDir stringByAppendingPathComponent:newDatabaseName]retain];
    // Execute the "checkAndCreateDatabase" function
    [self checkAndCreateDatabase:databasePath];
}

// Name         :   checkAndCreateDatabase:
// Description  :   Method checks and creates the database file at the given path if its not present.
// Arguements   :   NSString    : file path.
// Retrun       :   None.
-(void) checkAndCreateDatabase:(NSString *)dbPath
{
    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    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:dbPath];
    // 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] pathForResource:@"TestData" ofType:@"sqlite"];
    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:dbPath error:nil];
}

Also note that if we are using database so it is good practice to locate it first in some directory from where user can take backup. Documents directory of sandbox is one of these backup ready directory.

Thanks,

0

精彩评论

暂无评论...
验证码 换一张
取 消