我的表中有5列,其中3个值来自一个保存方法,2个来自另一个。当我从第一个保存方法插入数据时,前3列被填充,现在我想要其他2列由下一个更新方法填充,但在同一行。 我尝试使用ID进行更新,但这只是在特定行中更新。 P.S:我是iOS&的新手第一次使用sqlite。 在这里我做了什么: -
-(BOOL)createDB
{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: @"Calendardatabase.db"]];
NSLog(@"%@",databasePath);
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt =
"create table if not exists CalendarDataTable (ID INTEGER PRIMARY KEY AUTOINCREMENT,pl1 text, recordedTmpFile text,NavlabelDate text,eventTypeImgView text,eventNameLblP2 text)";
if (sqlite3_exec(database, sql_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(@"Failed to create table");
}
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(@"Failed to open/create database");
}
}
return isSuccess;
}
-(BOOL)saveData:(NSString*)pl1 recordedTmpFile:(NSString*)recordedTmpFile NavlabelDate:(NSString*)NavlabelDate eventTypeImgView:(NSString*)eventTypeImgView eventNameLblP2:(NSString*)eventNameLblP2;
//-(BOOL)saveData:(NSString*)eventTypeImgView eventNameLblP2:(NSString*)eventNameLblP2 pl1:(NSString*)pl1 pl2:(NSString*)pl2 pl3:(NSString*)pl3 pl4:(NSString*)pl4;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into CalendarDataTable (pl1,recordedTmpFile,NavlabelDate,eventTypeImgView,eventNameLblP2) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",pl1,recordedTmpFile,NavlabelDate,eventTypeImgView,eventNameLblP2];
// NSString *insertSQL = [NSString stringWithFormat:@"insert into CalendarDataTable (eventTypeImgView, eventNameLblP2 pl1 pl2 pl3 pl4) values (\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",eventTypeImgView,eventNameLblP2,pl1,pl2,pl3,pl4];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else
{
return NO;
}
// sqlite3_reset(statement);
}
return NO;
}
-(BOOL)UpdateTable:(NSString*)eventTypeImgView eventNameLblP2:(NSString*)eventNameLblP2;
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *updateSQL = [NSString stringWithFormat:@"UPDATE CalendarDataTable set eventTypeImgView='%@',eventNameLblP2='%@' where ID=1",eventTypeImgView,eventNameLblP2];
NSLog(@"%@",updateSQL);
const char *insert_stmt = [updateSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else
{
return NO;
}
// sqlite3_reset(statement);
}
return NO;
}
答案 0 :(得分:0)
如果您要问UpdateTable...
saveData...
方法中保存的行,您可以使用自定义标识符,该标识符以某种方式从saveData...
中插入的数据生成。
假设为名为CalendarDataTable
的{{1}}添加一个额外字段,并在其中存储一些唯一生成的值,例如当前时间戳created
,存储时间戳并使用它们{{1} } [NSDate timeIntervalSinceReferenceDate] * 1000