如果它已经在表中,如何不在sqlite中记录[关闭]

Posted

技术标签:

【中文标题】如果它已经在表中,如何不在sqlite中记录[关闭]【英文标题】:how to does not record in sqlite if it is already in table [closed] 【发布时间】:2013-07-30 07:27:35 【问题描述】:

我有一个应用程序,我在 sqlite 表中插入数据我希望如果数据已经插入,那么它不应该插入相同的记录这里是我插入数据的代码。

这是用于插入数据的带有插入语句的代码

       - (void) addCoffee:(NSString *)dbPath 

        if(addStmt == nil) 
        const char *sql = "insert into library(userID,contentAddedDateTime,contentType,contentTitle,contentSource,contentDescription,categoryTitle,subCategoryTitle,organizationCode,userName) Values(?,?,?,?,?,?,?,?,?,?)";

       if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 


       if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));


    



  sqlite3_bind_text(addStmt, 1, [userID UTF8String], -1, SQLITE_TRANSIENT);

  sqlite3_bind_text(addStmt, 2, [contentAddedDateTime UTF8String], -1, SQLITE_TRANSIENT);

  sqlite3_bind_text(addStmt, 3, [contentType UTF8String], -1, SQLITE_TRANSIENT);

  sqlite3_bind_text(addStmt, 4, [contentTitle UTF8String], -1, SQLITE_TRANSIENT);

  sqlite3_bind_text(addStmt, 5, [contentSource UTF8String], -1, SQLITE_TRANSIENT);

      sqlite3_bind_text(addStmt, 6, [contentDescription UTF8String], -1, SQLITE_TRANSIENT);


      sqlite3_bind_text(addStmt, 7, [categoryTitle UTF8String], -1, SQLITE_TRANSIENT);

      sqlite3_bind_text(addStmt, 8, [subCategoryTitle UTF8String], -1, SQLITE_TRANSIENT);

      sqlite3_bind_text(addStmt, 9, [organizationCode UTF8String], -1, SQLITE_TRANSIENT);
      sqlite3_bind_text(addStmt, 10, [userName UTF8String], -1, SQLITE_TRANSIENT);



if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    coffeeID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);


   

【问题讨论】:

什么不起作用?您是否尝试过返回行数以查看是否已插入记录? 【参考方案1】:
- (int) GetCount :(NSString *)UserID

[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];

int count = 0;


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dbfav2table.sqlite"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)

    sqlite3_stmt *statement;

    NSString *sql_str = [NSString stringWithFormat:@"SELECT count(*) FROM tablename WHERE userID='%@'", UserID];

    const char *sqlStatement = (char *)[sql_str UTF8String];
     NSLog(@"%@", [NSString stringWithUTF8String:sqlStatement]);

    if( sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
    

        while( sqlite3_step(statement) == SQLITE_ROW )
        
            count = sqlite3_column_int(statement, 0);
            NSLog(@"count for %@ is %i",UserID,count);
        

    
    else

 
        NSLog( @" Error is:  %s", sqlite3_errmsg(database) );

 

    // Finalize and close database.
    sqlite3_finalize(statement);
    sqlite3_close(database);


return count;

如果返回count = 1,则表示已经存在具有相同userId的值,否则不存在该值。希望对你有帮助

【讨论】:

【参考方案2】:

您应该编写具有唯一列的选择查询。让我们说 userID 在你的情况下

const char *sql = "select * from library where userID=%@";

如果记录存在则进入否则跳过

【讨论】:

以上是关于如果它已经在表中,如何不在sqlite中记录[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

使用while循环在表中插入多条记录[关闭]

如果表中的行存在,如何转义当前的 SQLite3 查询,但如果不存在则继续

SQLite:查询纬度/经度增量

不和谐 Java JDA |从 SQLite 删除数据/从 SQLite 获取数据 [关闭]

如何在 SQLite 中设置自动时间戳?

SQL查询用户在表中有一条记录但没有另一条[关闭]