c oracle分页

2023年 8月 4日 53.8k 0

5
6 OCIEnv *envhp;
7 OCIError *errhp;
8 OCISvcCtx *svchp;
9 OCIStmt *stmthp;
10 OCIDefine *defhp;
11
12 char *username = "scott";
13 char *password = "tiger";
14 char *dbname = "orcl";
15
16 void error_report(OCIError *errhp) {
17 text *bufp;
18 sb4 errcode;
19 OCIErrorGet((dvoid *)errhp, 1, NULL, &errcode, bufp, (ub4)sizeof(bufp), (ub4)OCI_HTYPE_ERROR);
20 printf("Error code = %d, Error message = %s\n", errcode, bufp);
21 }
22
23 int main() {
24 int result_count = 10;
25 int page_count = 1;
26 int total_count;
27 int i = 0;
28 char query_string[1024];
29 text *sqlstmt;
30 int sqlstmt_size;
31 int status;
32
33 OCIInitialize((ub4) OCI_DEFAULT, (dvoid *)0, (dvoid * (*)(dvoid *, size_t))0,
34 (dvoid * (*)(dvoid *, dvoid *, size_t))0, (void (*)(dvoid *, dvoid *))0);
35 OCIEnvInit(&envhp, OCI_DEFAULT, 0, 0);
36 OCIHandleAlloc((dvoid *)envhp, (dvoid **)&errhp, OCI_HTYPE_ERROR, 0, 0);
37 OCIHandleAlloc((dvoid *)envhp, (dvoid **)&svchp, OCI_HTYPE_SVCCTX, 0, 0);
38 OCIHandleAlloc((dvoid *)envhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, 0, 0);
39
40 if (OCILogon(envhp, errhp, &svchp, (OraText *)username, (ub4)strlen(username), (OraText *)password, (ub4)strlen(password), (OraText *)dbname, (ub4)strlen(dbname)) != OCI_SUCCESS) {
41 error_report(errhp);
42 return 1;
43 }
44
45 sprintf(query_string, "SELECT COUNT(*) FROM person");
46 OCIStmtPrepare(stmthp, errhp, (OraText *)query_string, (ub4)strlen(query_string), (ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
47 status = OCIStmtExecute(svchp, stmthp, errhp, (ub4)1, (ub4)0, (CONST OCISnapshot*)NULL, (OCISnapshot*)NULL, (ub4)OCI_DEFAULT);
48
49 if (status != OCI_SUCCESS) {
50 error_report(errhp);
51 return 1;
52 }
53
54 OCIHandleAlloc((dvoid *)envhp, (dvoid **)&defhp, OCI_HTYPE_DEFINE, 0, 0);
55 OCIStmtBindByName(stmthp, &defhp, errhp, (OraText *)"COUNT(*)", -1, (dvoid *) &total_count, sizeof(total_count), SQLT_INT, (dvoid *)0, (ub2 *)0, (ub2 *)0, 0, (ub4)OCI_DEFAULT);
56 OCIStmtFetch(stmthp, errhp, 1, OCI_FETCH_NEXT, OCI_DEFAULT);
57
58 if (result_counttotal_count) {
62 result_count = total_count;
63 }
64
65 page_count = (total_count + result_count - 1) / result_count;
66
67 sprintf(query_string, "SELECT * FROM person WHERE rownum >= %d AND rownum

以上代码中,我们首先使用OCI函数OCILogon连接Oracle数据库。然后使用OCI函数OCIStmtPrepare准备需要执行的SQL语句。接下来,我们可以使用OCI函数OCIStmtExecute执行SQL语句并返回结果集。这里需要注意的是,对于分页操作,我们需要使用rownum来筛选结果集,并且需要计算出总页数。

可以看到,C Oracle分页的实现并不复杂,只需要使用几个简单的API函数即可。当然,在实际的应用中,根据具体情况需要做出相应的修改。

总之,C Oracle分页是一个非常实用的技术,可以让我们更加方便地处理数据库中的大型数据集合。希望本文能够对您有所帮助!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论