SQLite的REPLACE函数在目标c ios上不起作用
我正在处理SQLite,但当我替换它一次又一次添加到数据库中的数据时,基本上我想存储SQLite的REPLACE函数在目标c ios上不起作用,ios,objective-c,sqlite,replace,Ios,Objective C,Sqlite,Replace,我正在处理SQLite,但当我替换它一次又一次添加到数据库中的数据时,基本上我想存储报警名称,报警类型和报警调谐路径,在NotiType中应该只有6行,因为我在每列中插入了6个元素。但这里的问题是,当我每天都在更换报警名称、类型和调谐时的数据,但数据被插入到下面的每一列中,并在表调用replace data函数时计数 if([fileManager fileExistsAtPath:_dbPath] == YES) { const char *dbPathagain = [_dbPath
报警名称
,报警类型
和报警调谐路径
,在NotiType
中应该只有6行
,因为我在每列中插入了6个元素。但这里的问题是,当我每天都在更换报警名称、类型和调谐时的数据,但数据被插入到下面的每一列中,并在表调用replace data函数时计数
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));
}
}
if([fileManager fileExistsAtPath:\u dbPath]==YES){
const char*dbpathreach=[\u dbPath UTF8String];
if(sqlite3_打开(dbpathReach,&_DB)==SQLITE_正常){
NSLog(@“数据库已打开”);
字符*错误消息;
const char*sql_statement=“如果不存在,则创建表NotiType(报警文本、类型文本、TPath文本)”;
NSLog(@“创建表成功”);
//sqlite3_stmt*语句=NULL;
//const char*dbpathreach=[\u dbPath UTF8String];
如果([fileManager fileExistsAtPath:_dbPath]==是){
sqlite3_stmt*语句=NULL;
const char*dbpathreach=[\u dbPath UTF8String];
if(sqlite3_打开(dbpathReach,&_DB)==SQLITE_正常){
对于(int-itemIndex=0;itemIndex<[azanst计数];itemIndex++){
NSString*myname=[azanst objectAtIndex:itemIndex];
NSString*mytype=[alarmsstype objectAtIndex:itemIndex];
NSString*mytunepath=[alarmtune objectAtIndex:itemIndex];
NSString*insertSQLData=[NSString stringWithFormat:@“替换为NotitType(报警、类型、TPath)值(\“%@\”、\“%@\”、\“%@\”)、myname、mytype、mytunepath];
NSLog(@“此处是报警名称%@”,myname);
NSLog(@“此处报警类型%@”,mytype);
NSLog(@“此处报警调谐路径%@”,mytunepath);
常量字符*insert_语句=[insertSQLData UTF8String];
sqlite3\u prepare\u v2(\u DB,insert\u语句,-1,&语句,NULL);
if(sqlite3\u步骤(语句)==SQLITE\u完成){
NSLog(@“数据添加成功”);
}
否则{
NSLog(@“无法添加计时”);
}
}
sqlite3_最终确定(声明);
sqlite3_close(_DB);
}
}
if(sqlite3\u exec(\u DB,sql\u语句,NULL,NULL和errorMessage)!=SQLITE\u OK){
NSLog(@“未能在表中插入”);
}
sqlite3_close(_DB);
}
否则{
NSLog(@“无法打开db或cretate表”);
NSLog(@“数据库错误消息:%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"];
}
对于(i=0,i您忘记提供WHERE语句:
NSString*insertSQLData=[NSString stringWithFormat:@”替换为
NotiType(报警、类型、TPath)值(\“%@\”、\“%@\”、\“%@\”)、myname、,
mytype,mytunepath]
您应该添加诸如WHERE _id=%@、MyAlarmUniqiIdentifier之类的内容,以确保只更改正在编辑的内容
编辑:想一想,我也建议使用更新,我不确定替换是否是你想要的
UPDATE {table} SET {column1} = {editedObject.value1}, {column2} = {editedObject.value2} WHERE {uniqueIdField} = {editedObject.identifier};
编辑2:
编辑后,按如下方式更新for循环:
NSString *myname = [azanst objectAtIndex:i];
NSString *mytype = [alarmsstype objectAtIndex:i];
NSString *mytunepath = [alarmtune objectAtIndex:i];
NSNumber *myid = [ID objectAtIndex:i]; //<-- get the unique identifier for the item number ‘i‘
[NSString stringWithFormat:@"UPDATE NotiType SET Alarm=%@, Type=%@, Tpath=%@ where ID=%@", myname, mytype, mytunepath, myid];
NSString*myname=[azanst objectAtIndex:i];
NSString*mytype=[alarmsstype对象索引:i];
NSString*mytunepath=[alarmtune对象索引:i];
NSNumber*myid=[ID objectAtIndex:i];//如果要访问REPLACE
键,请将任何一个键设置为主键,哪个键应为主键??选择是您的,您的表主标识,例如登录页(用户电子邮件是主键)是主键每个数据都在更新实际上,每列中有六个元素,每天每六个元素都在更改,但行数不应超过sixReplace将替换所有行。因此,如果没有WHERE子句,您将用相同的值覆盖数据库的所有字段。我假设这不是您想要的,因此您需要创建e将包含唯一标识符的字段(自动递增)更新每一行标识符=RotoBeEdEddi.IdistFar,但是我应该做ID自动增量吗?我编辑了我的答案,我认为你应该考虑使用更新而不是更新。而且,你的ID字段应该是自动增量的,除非你想添加一些逻辑来处理它自己,只需添加一个属性整数自动列。create语句中的增量但最后一个查询对我来说似乎不明确