1

首先我是新来的,所以我要感谢在我的项目中帮助我很多的社区。

我实际上正在开发一个必须从本地 sqlite db 提供客户信息的应用程序。

我做了以下事情:

  • 启动应用程序时,我复制了 sqlite 数据库(如果文档文件夹中不存在数据库)

  • 当用户在界面中请求更新时,会调用一个方法并填写数据库中的 customer_information 表。

  • 当用户返回主屏幕时,会调用一个方法并根据 sqlite 数据库中的值刷新显示的信息。

问题是没有显示任何值。XCode 在我的代码中找不到任何错误,即使我尝试了许多不同的插入值的方法,我也找不到问题出在哪里。

提前谢谢你,对不起我的英语不好,我是法国人。

真挚地。

请从我的源代码中找到以下摘录,您有任何问题请不要犹豫:

复制文档文件夹中的数据库

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSError *emsg;
    BOOL fileExist;

    //Get list of directories in Document path
    NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    //Define new path for database
    NSString *documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"Vidagen_ios.sqlite"];

    // Path of db in the application
    NSString *origin = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Vidagen_ios.sqlite"];

    fileExist = [[NSFileManager defaultManager] fileExistsAtPath:documentPath];

    if(!fileExist){
        [[NSFileManager defaultManager] copyItemAtPath:origin toPath:documentPath error:&emsg];
        NSLog(@"db copied");
    }

    // Override point for customization after application launch.
    return YES;
}

更新方法

- (void)insertRecords
{
    sqlite3_stmt *sqlStatement = nil;
    sqlite3 *newDb;

    // Request
    const char *sql = "INSERT INTO client_information(client_info_id, client_info_name, client_info_sex, client_info_age, client_info_id_card, client_info_tel, client_info_tall, client_info_weight, client_info_address, client_info_email, client_info_pwd, client_info_client_source_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

    // Path of copied db
    NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *database = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"Vidagen_ios.sqlite"];

    // db sequence
    if(sqlite3_open([database UTF8String], &newDb) == SQLITE_OK){
        NSLog(@"ouverture de la base ok");
        for (int i=0; i<11; i++) {
            sqlite3_bind_text(sqlStatement, i, [[NSString stringWithFormat:@"Test"] UTF8String], -1, SQLITE_TRANSIENT);
        }
        sqlite3_bind_int(sqlStatement, 11, 1);
        sqlite3_prepare_v2(newDb, sql, 1, &sqlStatement, NULL);
        if (sqlite3_step(sqlStatement) == SQLITE_OK) {
            NSLog(@"client ajouté");
        } else{
            NSLog(@"Echec d'ajout");
        }
        sqlite3_step(sqlStatement);
        sqlite3_finalize(sqlStatement);
        sqlite3_close(newDb);
    };

}

这是我注意到该步骤与“SQLITE_OK”不同。

显示信息的方法

- (void)viewDidLoad
{
    infoCustomer = [[CustomerDAO alloc] init];
    if([[infoCustomer getCurrentCustomer] count] > 0){
        self.customer = [[infoCustomer getCurrentCustomer] objectAtIndex:0];

        [self.uniqueId setText:self.customer.uniqueId];
        [self.name setText:self.customer.name];
        [self.sex setText:self.customer.sex];
        [self.tall setText:self.customer.tall];
        [self.weight setText:self.customer.weight];
        [self.age setText:self.customer.age];
        [self.address setText:self.customer.address];
        [self.tel setText:self.customer.tel];
        [self.email setText:self.customer.email];
    } else{
        NSLog(@"customer not read");
        [self.uniqueId setText:@"Go to Update."];
        [self.name setText:@""];
        [self.sex setText:@""];
        [self.tall setText:@""];
        [self.weight setText:@""];
        [self.age setText:@""];
        [self.address setText:@""];
        [self.tel setText:@""];
        [self.email setText:@""];
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

编辑 :

我从昨天开始继续我的研究,虽然我的源代码有问题:

sqlite3_prepare_v2(newDb, sql, 1, &sqlStatement, NULL);

第三个参数必须更改为 -1,因为它定义了我的请求的长度。我使用了一些教程来学习 sqlite 请求,我并没有真正花时间去理解我看到的所有参数。如果将其定义为 -1,则表示长度将在运行时自动计算。

在这样的错误上浪费了这么多时间,我感到很沮丧,这向我展示了在没有足够经验或没有外部意见的情况下工作是多么困难。

4

0 回答 0