sqlite中数据插入问题

发布于 2024-10-25 19:55:34 字数 1566 浏览 0 评论 0原文

我的表定义是: CREATE TABLE "Product" ("ProID" INTEGER PRIMARY KEY AUTOINCRMENT NOT NULL , "BarCode" VARCHAR NOT NULL UNIQUE , "ProductName" VARCHAR NOT NULL );

当我插入数据时,我遇到这样的问题: 2011-03-25 15:43:08.792 BarCode12[6698:207] * -[sqlClass addRecord:] 中的断言失败,/Users/admin/Desktop/BarCode14copy/Classes/sqlClass.m:121 2011-03-25 15:43:08.793 BarCode12[6698:207] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“插入数据时出错。 '约束失败'' ...

编写的代码是: - (void) addRecord:(NSMutableDictionary *)recordDict {

if(addStmt == nil) {
    const char *sql = "insert into Product(ProID, BarCode , ProductName ) Values(?,?,?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
NSInteger recordId =  [[recordDict objectForKey:@"ProID"] intValue];
NSLog(@"%d",recordId);
sqlite3_bind_int(addStmt, 1,recordId);
sqlite3_bind_text(addStmt, 2, [[recordDict objectForKey:@"BarCode"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [[recordDict objectForKey:@"ProductName"] UTF8String], -1, SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    sqlite3_last_insert_rowid
    rowID = sqlite3_last_insert_rowid(database);
NSLog(@"last inserted rowId = %d",rowID);

sqlite3_reset(addStmt);

}

我也在没有限制的情况下进行了尝试,但同样的问题将继续存在。

如果一致性被删除,那么它会显示“数据库锁定”错误。 我们如何消除这个错误以及如何将数据插入数据库?

My table definition is: CREATE TABLE "Product" ("ProID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , "BarCode" VARCHAR NOT NULL UNIQUE , "ProductName" VARCHAR NOT NULL );

when i am inserting data i have the problem like:
2011-03-25 15:43:08.792 BarCode12[6698:207] * Assertion failure in -[sqlClass addRecord:], /Users/admin/Desktop/BarCode14copy/Classes/sqlClass.m:121
2011-03-25 15:43:08.793 BarCode12[6698:207] *
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while inserting data. 'constraint failed''
...

The code written is: - (void) addRecord:(NSMutableDictionary *)recordDict
{

if(addStmt == nil) {
    const char *sql = "insert into Product(ProID, BarCode , ProductName ) Values(?,?,?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
NSInteger recordId =  [[recordDict objectForKey:@"ProID"] intValue];
NSLog(@"%d",recordId);
sqlite3_bind_int(addStmt, 1,recordId);
sqlite3_bind_text(addStmt, 2, [[recordDict objectForKey:@"BarCode"] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 3, [[recordDict objectForKey:@"ProductName"] UTF8String], -1, SQLITE_TRANSIENT);


if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    sqlite3_last_insert_rowid
    rowID = sqlite3_last_insert_rowid(database);
NSLog(@"last inserted rowId = %d",rowID);

sqlite3_reset(addStmt);

}

i am also tried without constraints but the same problem will continues..

if the consistency is removed then it shows "database Locked" error..
how can we remove this error and how to insert the data into database?

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

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

发布评论

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

评论(1

撧情箌佬 2024-11-01 19:55:34

关于“约束失败”的部分表示您违反了约束。这意味着您的 Product 表具有 PRIMARY KEY、UNIQUE 或 FOREIGN KEY 约束,并且您插入的值会违反这些约束。

  • 对于 PRIMARY KEY 和 UNIQUE 约束:您正在插入重复的值。
  • 对于 FOREIGN KEY 约束:您插入的值在引用的表/字段中没有相应的匹配项。

更新:当您有 INTEGER PRIMARY KEY AUTOINCRMENT 时,您不应在 INSERT 语句中指定 ProdID; SQLite 会自动生成它。因此,将您的插入语句更改为,

INSERT INTO Product (BarCode , ProductName) Values (?,?)

因为您之后已经检索了它,

rowID = sqlite3_last_insert_rowid(database);

不要忘记也删除绑定!

The bit about "constraint failed" says that you are violating a constraint. That means your Product table has PRIMARY KEY, UNIQUE or FOREIGN KEY constraints, and that you’re inserting values which would violate those constraints.

  • For PRIMARY KEY and UNIQUE constraints: you’re inserting a duplicate value.
  • For FOREIGN KEY constraints: you’re inserting a value that has no corresponding match in the referenced table/field.

Update: When you have an INTEGER PRIMARY KEY AUTOINCREMENT, then you shouldn’t specify a ProdID in your INSERT statement; SQLite will generate it automatically. So change your insert statement to

INSERT INTO Product (BarCode , ProductName) Values (?,?)

since your already retrieve it afterward with

rowID = sqlite3_last_insert_rowid(database);

Don’t forget to remove the binding, too!

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