【发布时间】:2018-09-09 12:09:04
【问题描述】:
我正在使用 SQLite,但是当我替换它一次又一次添加到我的数据库中的数据时,基本上我想存储 alarm name、alarm type 和 alarm tune path,应该只有 6 rows 在NotiType 因为我在每一列中都插入了 6 个元素。但是这里的问题是当我每天更换警报名称类型和调整时的数据时,但是数据被插入到下面的每一列中,并且当表格调用替换数据函数时计数。
if([fileManager fileExistsAtPath:_dbPath] == YES) {
const char *dbPathagain = [_dbPath UTF8String];
if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
NSLog(@"database is open");
char *errorMessage;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS NotiType(Alarm TEXT, Type TEXT, TPath TEXT)";
NSLog(@"created table success");
// sqlite3_stmt *statement = NULL;
// const char *dbPathagain = [ _dbPath UTF8String];
if([fileManager fileExistsAtPath:_dbPath] == YES) {
sqlite3_stmt *statement = NULL;
const char *dbPathagain = [_dbPath UTF8String];
if(sqlite3_open(dbPathagain, &_DB) == SQLITE_OK ) {
for(int itemIndex = 0; itemIndex < [azanst count];itemIndex++){
NSString *myname = [azanst objectAtIndex:itemIndex];
NSString *mytype = [alarmsstype objectAtIndex:itemIndex];
NSString *mytunepath = [alarmtune objectAtIndex:itemIndex];
NSString *insertSQLData = [NSString stringWithFormat:@"REPLACE INTO NotiType(Alarm, Type, TPath) VALUES (\"%@\", \"%@\",\"%@\")", myname, mytype, mytunepath];
NSLog(@"here is alarm names %@", myname);
NSLog(@"here alarm types %@", mytype);
NSLog(@"here alarm tune path %@", mytunepath);
const char *insert_statement = [insertSQLData UTF8String];
sqlite3_prepare_v2(_DB, insert_statement, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
NSLog(@"data added successfully");
}
else {
NSLog(@"could not add timings");
}
}
sqlite3_finalize(statement);
sqlite3_close(_DB);
}
}
if (sqlite3_exec(_DB, sql_statement, NULL, NULL, &errorMessage) != SQLITE_OK) {
NSLog(@"failed to insert in table");
}
sqlite3_close(_DB);
}
else {
NSLog(@"failed to open db or cretate table");
NSLog(@"Database Error Message : %s", sqlite3_errmsg(_DB));
}
}
更新
我正在做这样的事情
for (i=0, i<= ID.count, i++) {
NSString *insertSQLData = [NSString stringWithFormat:@"UPDATE NotiType SET Alarm =" + myname "," + "Type =" + mytype "," + "Tpath =" mytunepath + " where ID == i"];
}
【问题讨论】:
-
如果你想访问
REPLACE键然后设置任何一个键作为主键 -
哪个应该是主键??
-
选择是你的,你的表主标识例如登录页面(用户电子邮件是主键)是主键
标签: ios objective-c sqlite replace