sqlite中数据插入问题
我的表定义是: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于“约束失败”的部分表示您违反了约束。这意味着您的
Product
表具有 PRIMARY KEY、UNIQUE 或 FOREIGN KEY 约束,并且您插入的值会违反这些约束。更新:当您有 INTEGER PRIMARY KEY AUTOINCRMENT 时,您不应在 INSERT 语句中指定 ProdID; SQLite 会自动生成它。因此,将您的插入语句更改为,
因为您之后已经检索了它,
不要忘记也删除绑定!
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.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
since your already retrieve it afterward with
Don’t forget to remove the binding, too!