将数据写入 sqlite xCode [英] Writing data to sqlite xCode

查看:20
本文介绍了将数据写入 sqlite xCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我来自西班牙,对我的语法感到抱歉.我正在将一些数据写入 sqlite 数据库,这是我的代码:

first of all I am from spain so sorry about my grammar. I am writing some data to a sqlite data base, here is my code:

@try {

    NSFileManager *fileMgr=[NSFileManager defaultManager];

    NSString *dbPath=[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"capturas.sqlite"];
    BOOL succes=[fileMgr fileExistsAtPath:dbPath];
    if(!succes)
    {
        NSLog(@"Cannot locate database '%@'.",dbPath);
    }
    if (!(sqlite3_open([dbPath UTF8String], &dbcapturas)==SQLITE_OK)) {
        NSLog(@"An error has occured: %@",sqlite3_errmsg(dbcapturas));
    }

    //sqlite3_stmt *sqlStatement;
    NSString *asd=numero.text;
    NSString *insertStatement=[NSString stringWithFormat:@"INSERT INTO captura(key,tecnico, fecha,provincia,municipio,latitud,longitud,altura,familia,especie,numero,comentario)Values(\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",asd,tecnico,fechaHora,tecnico,municipio,latitud,longitud,altura,tecnico,animal,asd,coment];
    char *error;
    if((sqlite3_exec(dbcapturas, [insertStatement UTF8String], NULL, NULL, &error))==SQLITE_OK)
    {
        NSLog(@"Person inserted.");
    }
    else
    {
        NSLog(@"Error: %s", error);
    }
} @catch (NSException *exception) { 
    NSLog(@"fail"); 
}
@finally {

}

我第一次点击保存按钮时:

the first time I click on the save button I get:

2012-07-04 12:17:45.644 adasdasd[1783:f803] 插入的人.

2012-07-04 12:17:45.644 adasdasd[1783:f803] Person inserted.

我第二次得到:

2012-07-04 12:29:18.959 adasdasd[1840:f803] 错误:列键不唯一

2012-07-04 12:29:18.959 adasdasd[1840:f803] Error: column key is not unique

所以我的代码应该没问题,但是当我打开数据库时它完全是空的,知道吗?

So my code should be ok but when I open the database its totally empty, any idea?

推荐答案

您的主包中有 SQLITE db 文件.主包中的所有文件都是只读的.您需要将数据库复制到 Documents 文件夹.

You have your SQLITE db file in your main bundle. All files in the main bundle are read-only. You need to copy the db to the Documents folder.

试试这个(来自 http://www.iphonedevsdk.com/forum/iphone-sdk-development/17262-sqlite-database-works-fine-on-simulator-but-is-readonly-on-device.html):

- (void)createEditableCopyOfDatabaseIfNeeded
{
// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@\"database_name.sql\"];
success = [fileManager fileExistsAtPath:writableDBPath];
// NSLog(@\"path : %@\", writableDBPath);
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@\"database_name.sql\"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
{
NSAssert1(0, @\"Failed to create writable database file with message '%@'.\", [error localizedDescription]);
}
}

这篇关于将数据写入 sqlite xCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆