无法将数据插入sqlite表

时间:2011-04-14 10:15:01

标签: iphone objective-c

我是iphone开发的新手,我试图在sqlite表中插入数据。但数据未插入表中。控制台上没有错误。

NSString * string1 = @“测试”;     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));
            }

        }

1 个答案:

答案 0 :(得分:1)

作为资源包中的数据库,您正在尝试修改和更新它。更好的方法是将数据库第一次放在沙箱的文档目录中,然后对该数据库执行操作。

以下是使用该方法将数据库移动到应用程序沙箱的文档目录的方法。使用数据库操作时,此方法只应调用一次。(因为只有第一次我们需要将它放在该位置,有时我们只需要访问它)。

代码

// 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];
}

另请注意,如果我们使用数据库,那么最好先将它放在用户可以进行备份的某个目录中。沙箱的文档目录是这些备份就绪目录之一。

谢谢,