我想创建一个名为“movies”的表,其中包含两列,“movieId(主键和自动增量)”和“名称(文本)”
sql = [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXISTS 'movies' ('movieId' " "INT AUTO INCREMENT PRIMARY KEY , 'name' VARCHAR(255) NULL)"];
if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Table failed to create : movies");
}
我正在尝试插入一些数据
sql = [NSString stringWithFormat:@"INSERT INTEGERO movies('name') VALUES('movie name')"];
if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0, @"Insert fail");
}
我得到了这个:
2014-03-24 22:47:59.634 ProjectName[418:60b] *** Assertion failure in -[Data insertRecords], /Users/name/Objective-C Apps ios7/ProjectName/ProjectName/Data.m:108
2014-03-24 22:47:59.637 ProjectName[418:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Failed Insert'
*** First throw call stack:
(0x2d6e1fd3 0x3838bccf 0x2d6e1ead 0x2e08ed5b 0x19ef9 0x18eef 0xfc19 0x2ff024cb 0x2ff02289 0x2ff79525 0x2ff77ea5 0x2ff779f3 0x2ff7797b 0x2ff77913 0x2ff6ff89 0x2ff04127 0x2ff77661 0x2ff77125 0x2ff09065 0x2ff06847 0x2ff7035d 0x2ff6cfcd 0x2ff6758b 0x2ff03709 0x2ff02871 0x2ff66cc9 0x32549aed 0x325496d7 0x2d6acab7 0x2d6aca53 0x2d6ab227 0x2d615f4f 0x2d615d33 0x2ff65ef1 0x2ff6116d 0x22a1d 0x38898ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
如果从create table命令中删除“Auto Increment”,它会很有效。如何使用自动增量列创建表并插入数据?
答案 0 :(得分:1)
您不需要使用自动增量。
sql = [NSString stringWithFormat:
@"CREATE TABLE IF NOT EXISTS 'movies' ('movieId' " "INTEGER PRIMARY KEY, 'name' VARCHAR(255) NULL)"];
if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0, @"Table failed to create : movies");
}
什么是" INTEGERO"?
sql = [NSString stringWithFormat:@"INSERT INTO movies('movieId','name') VALUES(null, 'movie name')"];
if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) !=SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0, @"Insert fail");
}