我的第一个关于stackoverflow的问题,我真的希望你们能为我提供帮助。
到目前为止,我一直在尝试使准备好的语句在目标C中运行。
但是现在尝试添加轨道时,数据库上始终出现“约束失败”的信息。
我已经删除了数据库并再次创建它,但是错误仍然存在。
另外,我一直在研究重复键,但是我看不到我在任何地方添加重复键。
位置表添加得很好,并且它的构建方式相同。
我的数据库:
CREATE TABLE locations
(LocationID INTEGER PRIMARY KEY AUTOINCREMENT,
TimestampGPS DATE NOT NULL,
Longitude REAL NOT NULL,
Latitude REAL NOT NULL,
Altitude REAL NOT NULL,
Accuracy REAL NOT NULL,
Bearing REAL NOT NULL,
Speed REAL NOT NULL,
TimestampDevice DATE NOT NULL);
CREATE TABLE tracks
(TrackID INTEGER PRIMARY KEY AUTOINCREMENT,
Title VARCHAR,
Description VARCHAR,
StartTrackDeviceTime DATE NOT NULL,
EndTrackDeviceTime DATE NOT NULL);
CREATE TABLE track_locations
(TrackID INTEGER NOT NULL REFERENCES tracks(TrackID),
LocationID INTEGER NOT NULL REFERENCES locations(LocationID),
PRIMARY KEY (TrackID, LocationID));
我的代码添加了曲目。 (我有代码来添加类似的位置,并且可以正常工作)
const char *sqlTrack = "INSERT INTO tracks(Title, Description, StartTrackDeviceTime, EndTrackDeviceTime) VALUES(?, ?, ?, ?)";
if(sqlite3_prepare_v2(db.dataBase, sqlTrack, -1, &addTrackStatement, NULL) != SQLITE_OK) {
NSLog(@"Error: %s", sqlite3_errmsg(db.dataBase));
}
sqlite3_bind_text(addTrackStatement, 1, [track.title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addTrackStatement, 2, [track.description UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addLocationStatement, 3, [[dateFormatter stringFromDate:track.startTime] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addTrackStatement, 4, [[dateFormatter stringFromDate:track.endTime] UTF8String], -1, SQLITE_TRANSIENT);
NSLog(@"Going to execute the statement");
if(SQLITE_DONE != sqlite3_step(addTrackStatement)) {
NSLog(@"First step failed");
NSLog(@"Error: %s", sqlite3_errmsg(db.dataBase));
return NO;
}
else {
long insertedTrack = sqlite3_last_insert_rowid(db.dataBase);
NSLog(@"Inserted id on TRACK: %ld", insertedTrack);
//HERE I ALSO ADD LOCATIONS
sqlite3_clear_bindings(addTrackStatement);
sqlite3_reset(addTrackStatement);
}
最佳答案
该行应:
sqlite3_bind_text(addLocationStatement, 3, [[dateFormatter stringFromDate:track.startTime] UTF8String], -1, SQLITE_TRANSIENT);
读:
sqlite3_bind_text(addTrackStatement, 3, [[dateFormatter stringFromDate:track.startTime] UTF8String], -1, SQLITE_TRANSIENT);
关于objective-c - SQLITE3约束失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10224023/