我创建了一个包含少量字段的表作为UNIQUE。因此,我必须在用户将重复的foldername插入表格时向用户显示警告。任何人都可以告诉我"如何检查现有数据,然后才能将其插入sqlite数据库的表格中#34;
答案 0 :(得分:3)
您可以通过以下代码检查记录
-(BOOL)CheckForRecord:(NSString *)Name
{
BOOL isRecordPresent=false;
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
@"SELECT ID FROM TBL_Name WHERE Column_Name=\"%@\"",Name];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_ROW)
{
isRecordPresent = 1;
}
else
{
isRecordPresent = 0;
}
}
}
sqlite3_finalize(statement);
if(sqlite3_close(database) == SQLITE_OK)
{
}
else
{
NSAssert1(0, @"Error: failed to close database on memwarning with message '%s'.", sqlite3_errmsg(database));
}
return isRecordPresent;
}