在 Xcode 中插入问题

发布于 2024-10-30 05:22:09 字数 971 浏览 1 评论 0原文

实际上我不确定是否存在问题,但是,我有一些数据,我想将其插入数据库。

代码似乎运行良好,每个语句都有效,没有显示错误,但我看不到数据库中的数据。它不会将数据插入数据库,但在代码中显示为插入数据。

是否有与数据库相关的设置?我正在使用 Sqlite Firefox 插件。 选择记录时没有问题。我检查了 .sqlite 文件的权限,但它设置为读取和读取写。

抱歉,我忘记添加代码片段;

   NSString *stmt=[NSString stringWithFormat:@"INSERT INTO t_exam_applies(user_id,exam_id, apply_correct, apply_wrong, apply_empty, apply_start, apply_end) VALUES(%d, %d, %d, %d, %d, %d, %d)", 0,exam_id,0,0,0,0,0];
        [stmt UTF8String];
        const char *sql=(const char *) [stmt UTF8String];
        //const char *sql= "INSERT INTO t_exam_applies(user_id,exam_id, apply_correct, apply_wrong, apply_empty, apply_start, apply_end) VALUES(?,?,?,?,?,?,?)";

    sqlite3_stmt *statement;

    sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_DONE)
        NSLog(@"SQLITE statement executed");
    else 
        NSLog(@"ERROR!!");

    sqlite3_finalize(statement);

Actually I am not sure there is a problem but, I have some data and I want to insert it to database.

Code seems to works well, every statement works, there are no errors shown but I cant see the data in database. It doesn't insert the data to database but in code it is shown as data is inserted.

Is there a setting related with database? I am using Sqlite Firefox Add-on.
When selecting records there are no problems. I checked the .sqlite file's permissions but it is setted to read & write.

Sorry, I forgot to add code snippet;

   NSString *stmt=[NSString stringWithFormat:@"INSERT INTO t_exam_applies(user_id,exam_id, apply_correct, apply_wrong, apply_empty, apply_start, apply_end) VALUES(%d, %d, %d, %d, %d, %d, %d)", 0,exam_id,0,0,0,0,0];
        [stmt UTF8String];
        const char *sql=(const char *) [stmt UTF8String];
        //const char *sql= "INSERT INTO t_exam_applies(user_id,exam_id, apply_correct, apply_wrong, apply_empty, apply_start, apply_end) VALUES(?,?,?,?,?,?,?)";

    sqlite3_stmt *statement;

    sqlite3_prepare_v2(database, sql, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_DONE)
        NSLog(@"SQLITE statement executed");
    else 
        NSLog(@"ERROR!!");

    sqlite3_finalize(statement);

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

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

发布评论

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

评论(2

转角预定愛 2024-11-06 05:22:09

鉴于这是针对 iPhone 开发的标记,我建议使用 sqlite 的目标 C 包装器,例如 FMDB。 FMDB 非常轻量级且使用广泛(在 github 上查看:https://github.com/ccgus/fmdb/ )并将为您简化整个过程。

以下是使用 FMDB 进行简单插入的示例:

self.db = [FMDatabase databaseWithPath:fullPath];

 if (![db open]) {
     NSLog(@"DB Open Failed");
     return NO;
 }

[db executeUpdate:@"INSERT INTO TestTable (id, value) VALUES (?,?)", [NSNumber numberWithInt:1], @"SomeString"];

[db close];

Given that this is tagged for iPhone dev I would recommend using an objective c wrapper for sqlite like FMDB. FMDB is very lightweight and widely used (check it out on github: https://github.com/ccgus/fmdb/) and will simplify this whole process for you.

Here is an example of a simple insert with FMDB:

self.db = [FMDatabase databaseWithPath:fullPath];

 if (![db open]) {
     NSLog(@"DB Open Failed");
     return NO;
 }

[db executeUpdate:@"INSERT INTO TestTable (id, value) VALUES (?,?)", [NSNumber numberWithInt:1], @"SomeString"];

[db close];
难得心□动 2024-11-06 05:22:09

也许您在添加数据时消息为零(假设您使用 Obj-C)?这不是一个错误,但它也没有做任何事情。更具体的答案需要更多信息或代码片段。

Maybe you are messaging nil when adding the data (assuming you use Obj-C)? That's not an error, but it also doesn't do anything. More info or code snippet needed for more specific answer.

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