RecordCount=-1问题

博客介绍了自主开发的BLOG网址,提及通常执行SQL语句的两种方法。因默认记录集游标是服务器游标,导致Rs.RecordCount返回-1,需将其改为客户端游标。还介绍了不同光标类型对应的recordcount属性情况,以及可用recordset.support进行属性支持测试。

自主开发的BLOG: http://www.ylhd.com/aa7643/DDS_Blog/

通常人们使用以下两种方法来执行SQL语句:
Set Rs=Conn.Execute(SqlStr)

Set Rs=Server.CreateObject(“ADODB.RecordSet“)
Rs.Open SqlStr,Conn,CursorType,LockType

(RecordSet对象方法请看这里


由于默认的记录集游标是服务器游标,
Rs.CursorLocation = adUseServer
所以返回Rs.RecordCount=-1,
应该把服务器游标改为客户端游标,
Rs.CursorLocation = adUseClient
Rs.Open SqlStr,Conn,CursorType,LockType

rs.cursortype

 光标类型                    recordcount 属性
---------------------------------------------
ForwardOnly                  0(默认) 返回-1
Keyset                            1         正确的记录数
Dynamic                         2         -1或不正确的记录数,依数据源而定
Static                              3         正确的记录数

所以Rs.CursorLocation = 3

可用recordset.support("属性名")进行测试是否支持该属性。

#include "employee_db2.h" #include <stdlib.h> #include <string.h> #include <time.h> #include <math.h> #include <pthread.h> #include <stdio.h> #define INIT_CAPACITY 10 #define SIMILARITY_THRESHOLD 0.8f typedef struct { DBRECORD *records; size_t recordCount; size_t capacity; pthread_mutex_t lock; } Database; // 创建新记录 DBRECORD* createDbRecord(const char* name, const char* id, const char *feature, const char *img, const size_t featureLength, const size_t imgWidth, const size_t imgHeight) { DBRECORD *record = malloc(sizeof(DBRECORD)); if (!record) return NULL; record->name = strdup(name); record->id = strdup(id); record->date = NULL; // 将在插入设置 record->feature = malloc(featureLength); memcpy(record->feature, feature, featureLength); //fprintf(stderr,"维度:%Iu\n", featureLength); //log record->featureLength = featureLength; record->img = malloc(imgWidth * imgHeight * 3); // 假设RGB图像 memcpy(record->img, img, imgWidth * imgHeight * 3); //printf("图片内存大小:%lu\n",imgWidth * imgHeight * 3); record->imgWidth = imgWidth; record->imgHeight = imgHeight; return record; } // 释放记录内存 void freeDbRecord(DBRECORD* dbRecord) { if (!dbRecord) return; if (dbRecord->name) free(dbRecord->name); if (dbRecord->id) free(dbRecord->id); if (dbRecord->date) free(dbRecord->date); if (dbRecord->feature) free(dbRecord->feature); if (dbRecord->img) free(dbRecord->img); } // 计算余弦相似度 float cosine_similarity(const char* v1, const char* v2, size_t length) { float dot = 0.0, denom_a = 0.0, denom_b = 0.0; for (size_t i = 0; i < length / sizeof(float); ++i) { float a = ((float*)v1)[i]; float b = ((float*)v2)[i]; dot += a * b; denom_a += a * a; denom_b += b * b; } return dot / (sqrt(denom_a) * sqrt(denom_b)); } // 数据库操作实现 static int db_insert(DBHANDLE* obj, const DBRECORD *record) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); // 检查容量并扩容 if (db->recordCount >= db->capacity) { size_t new_capacity = db->capacity * 2; DBRECORD *new_records = realloc(db->records, new_capacity * sizeof(DBRECORD)); if (!new_records) { pthread_mutex_unlock(&db->lock); return -1; } db->records = new_records; db->capacity = new_capacity; } // 设置当前间 time_t now = time(NULL); char *date_str = malloc(20); strftime(date_str, 20, "%Y-%m-%d %H:%M:%S", localtime(&now)); // 复制记录 DBRECORD *new_rec = &db->records[db->recordCount]; *new_rec = *record; new_rec->date = date_str; db->recordCount++; pthread_mutex_unlock(&db->lock); return 0; } static int db_searchWithId(DBHANDLE* obj, const char* id, DBRECORD **outResults, int *matchCount) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); // 第一阶段:统计匹配记录数量 int count = 0; for (size_t i = 0; i < db->recordCount; i++) { if (0 == strcmp(db->records[i].id, id)) { count++; } } // 无匹配记录直接返回 if (count == 0) { pthread_mutex_unlock(&db->lock); *matchCount = 0; *outResults = NULL; return -1; } // 动态分配结果数组 *outResults = (DBRECORD*)malloc(count * sizeof(DBRECORD)); if (*outResults == NULL) { pthread_mutex_unlock(&db->lock); return -2; // 内存分配失败 } // 第二阶段:填充匹配记录 int index = 0; for (size_t i = 0; i < db->recordCount; i++) { if (0 == strcmp(db->records[i].id, id)) { (*outResults)[index] = db->records[i]; // 结构体拷贝 index++; } } *matchCount = count; pthread_mutex_unlock(&db->lock); return 0; // 成功返回 } static int db_searchWithName(DBHANDLE* obj, const char* name, DBRECORD **outResults, int *matchCount) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); // 第一阶段:统计匹配记录数量 int count = 0; for (size_t i = 0; i < db->recordCount; i++) { if (0 == strcmp(db->records[i].name, name)) { count++; } } // 无匹配记录直接返回 if (count == 0) { pthread_mutex_unlock(&db->lock); *matchCount = 0; *outResults = NULL; return -1; } // 动态分配结果数组 *outResults = (DBRECORD*)malloc(count * sizeof(DBRECORD)); if (*outResults == NULL) { pthread_mutex_unlock(&db->lock); return -2; // 内存分配失败 } // 第二阶段:填充匹配记录 int index = 0; for (size_t i = 0; i < db->recordCount; i++) { if (0 == strcmp(db->records[i].name, name)) { (*outResults)[index] = db->records[i]; // 结构体拷贝 index++; } } *matchCount = count; pthread_mutex_unlock(&db->lock); return 0; // 成功返回 } static int db_searchWithFeature(DBHANDLE* obj, const char* feature, const size_t length, DBRECORD *outResult) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); float max_sim = 0; int best_match = -1; for (size_t i = 0; i < db->recordCount; i++) { float sim = cosine_similarity(feature, db->records[i].feature, length < db->records[i].featureLength ? length : db->records[i].featureLength); if (sim > max_sim && sim > SIMILARITY_THRESHOLD) { max_sim = sim; best_match = i; } } if (best_match >= 0) { *outResult = db->records[best_match]; pthread_mutex_unlock(&db->lock); return 0; } pthread_mutex_unlock(&db->lock); return -1; } static int db_delete(DBHANDLE* obj, const char* id) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); for (size_t i = 0; i < db->recordCount; i++) { if (strcmp(db->records[i].id, id) == 0) { // 释放记录中的动态分配内存 freeDbRecord(&db->records[i]); // 移动数组元素 memmove(&db->records[i], &db->records[i+1], (db->recordCount - i - 1) * sizeof(DBRECORD)); db->recordCount--; pthread_mutex_unlock(&db->lock); return 0; } } pthread_mutex_unlock(&db->lock); return -1; } static int db_free(DBHANDLE* obj) { Database *db = (Database*)obj->internal; pthread_mutex_lock(&db->lock); for (size_t i = 0; i < db->recordCount; i++) { freeDbRecord(&db->records[i]); } free(db->records); pthread_mutex_unlock(&db->lock); pthread_mutex_destroy(&db->lock); free(db); return 0; } // 创建数据库句柄 int createDbHandle(DBHANDLE* obj) { Database *db = malloc(sizeof(Database)); if (!db) return -1; db->records = malloc(INIT_CAPACITY * sizeof(DBRECORD)); if (!db->records) { free(db); return -1; } db->recordCount = 0; db->capacity = INIT_CAPACITY; pthread_mutex_init(&db->lock, NULL); obj->internal = db; obj->insertRecord = db_insert; obj->searchWithId = db_searchWithId; obj->searchWithName = db_searchWithName; // 简化处理,同ID搜索 obj->searchWithFeature = db_searchWithFeature; obj->deleteRecord = db_delete; obj->free = db_free; return 0; } // 查询所有记录 QueryResult queryAllRecords(DBHANDLE* obj) { QueryResult result = {0}; // 实现略... return result; }这里互斥锁 的用途是什么
08-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值