当前位置:首页 » 编程语言 » execsqlcommit
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

execsqlcommit

发布时间: 2023-01-01 23:36:12

㈠ 分析大文本与图像数据在数据库内部的存储原理。

图像数据在数据库内部的存储原理:
XML 是文本型的数据交换结构,对于字符类型的文本交换非常的方便,实际工作中我们往往需要通过 XML 将二进制格式的图形图像信息数据进行数据交换。本文从介绍 BASE64 编码的原理入手,通过采用 C 语言编写 DB2 的嵌入存储过程,实现了在数据库内存中将文本格式的图片文件到二进制 BLOB 字段之间的转换,并且就性能优化等提出若干建议,该设计思路和程序可以广泛的应用到图像图形数据在 XML 的存储和转换。

--------------------------------------------------------------------------------
回页首
XML 存储图形图像的基本原理

XML 作为一种非常广泛的数据交换的载体被广泛的应用到了各行各业的数据交换中。对于图形图像数据的转换,需要采用 Base64 编码将二进制格式的图形图像信息转换成文本格式再进行传输。

Base64 编码转换的思想是通过 64 个 ASCII 字符码对二进制数据进行重新编码组合,即将需要转换的数据每三个字节(24 位)为一组,再将这 24 位数据按每组 6 位进行重新划分,在每组的最高 2 位填充 0 最终成一个完整的 8 位字节。如果所要编码的数据的字节数不是 3 的整数倍,需要在最后一组数据填充 1 到 2 个字节的 0 字节。例如:我们对 ABC 进行 BASE64 的编码,ABC 的编码值:A(65), B(66), C(67)。再取二进制 A(01000001)B(01000010)C(01000011)连接起来构成 010000010100001001000011,然后按 6 位为单位分成 4 个数据块并在最高位填充两个 0 后形成 4 个字节的编码后的值(00010000)(00010100)(00001001)(00000011)。再将 4 个字节的数据转换成十进制数为(16)(20)(19)(3)。最后根据 BASE64 给出的 64 个基本字符表,查出对应的 ASCII 码字符(Q)(U)(J)(D)。这里的值实际就是数据在字符表中的索引。

BASE64 字符表:



某项目的数据交换采用 XML 的为介质,XML 的结构包括个人基本信息:姓名、性别、相片等信息,其中相片信息是采用经过 BASE64 函数转换后的文本型数据,图像图形信息通过 BASE64 进行数据转换后,形成文本格式的数据类型,再将相应的数据存放到 XML 中,最终形成可供交换的文本型的 XML 数据结构。

XML 的数据结构如下所示:

<?xml version=”1.0” encoding=”UTF-8” ?>
<HeadInfo>
<TotalNum>10<TotalNum>
<TransDate>2007-10-18</TransDate>
</HeadInfo>
<Data>
<Name> 张三 </Name>
<Sex> 男 </Sex>
<Photo>/9j/4AAQSkZJRgABAQAAAQABAAD......</Photo>
<Data>

--------------------------------------------------------------------------------
回页首
相片数据在 DB2 嵌入式 C 程序的实现方法

该项目要求能够在 DB2 数据库中将相片数据存储为二进制 BLOB 格式。我们采用 DATASTAGE 进行 XML 数据加载,将 XML 中的姓名、性别等基本数据项加载到相应的字段,其中文本型的相片数据则加载到 CLOB 字段中,再按照 BASE64 的编码规则进行逆向转码,整个数据流程如下图所示:

图 1. 相片存储流程图

用户的相片每天的更新数据为 30 万条,而且每个相片的平均大于 32KB,为了获得最佳的数据库性能,选择采用 C 存储过程的方式开发了 BASE64 的转换函数。每次函数读取存储在 CLOB 字段的文本格式数据全部存储到内存中,并且通过 decode 函数在内存中进行转码,转码后再存入数据库中。

程序的清单 1 是逐行读取 CLOB 字段,并且调用 decode 函数进行转码;程序的清单 2 是 decode 函数的关键性代码。完整的程序见源代码下载部分。

清单 1. 读入 CLOB,写入 BLOB 字段

EXEC sql BEGIN DECLARE SECTION;
SQL TYPE IS CLOB(100 K) clobResume; //CLOB 结构体变量
SQL TYPE IS BLOB(100 K) blobResume; //BLOB 结构体变量

sqlint16 bobind;
sqlint16 lobind;
sqlint16 cobind;
sqlint32 idValue;

EXEC SQL END DECLARE SECTION;
int clob2bin(void)
{
// 声明 SQLCA 结构
struct sqlca sqlca;
int charNb;
int lineNb;
long n;
n=0;

// 定义数据库游标
EXEC SQL DECLARE c1 CURSOR WITH HOLD FOR
SELECT czrkxp_a
FROM CZRK_blob for update;
EXEC SQL OPEN c1;

// 活动 CLOB 字段的信息,已经 CLOB 字段的大小
EXEC SQL FETCH c1 INTO :clobResume:cobind;
// 循环读取 CLOB 字段,并且调用 DECODE 转码函数
while (sqlca.sqlcode != 100)
{
if (cobind < 0)
{
printf(“ NULL LOB indicated.\n”);
}
else
{
n++;
decode(); // 文本格式到二进制流的转码函数
printf(“\nCurrent Row =%ld”,n);
// 数据写入 BLOB 字段
EXEC SQL update czrk_blob set czrkxp_blob = :blobResume
where current of c1; ;
// 提交事务
EXEC SQL COMMIT;
}
EXEC SQL FETCH c1 INTO :clobResume:cobind ;
}
// 关闭游标
EXEC SQL CLOSE c1;
EXEC SQL COMMIT;
return 0;
}

清单 2. 文本文件到二进制文件的转换

void decode( void )
{
unsigned char in[4], out[3], v;
int I, len;
long j,k;
j = -1;
k=0;
// 将读入 CLOB 结构体变量的数据进行转换
while( j < clobResume.length){
for( len = 0, I = 0; I < 4 && ( j < clobResume.length ); i++ ) {
v = 0;
while((j < clobResume.length) && v == 0 ) {
j++;
v = (unsigned char) clobResume.data[j];
v = (unsigned char) ((v < 43 || v > 122) ? 0 : cd64[ v – 43 ]);
if( v ) {
v = (unsigned char) ((v == ‘$’) ? 0 : v – 61);
}
}
if( j < clobResume.length ) {
len++;
if( v ) {
in[ I ] = (unsigned char) (v – 1);
}
}
else {
in[i] = 0;
}
}
if( len ) {
decodeblock( in, out );
// 写入到 BLOB 结构体变量中
for( I = 0; I < len – 1; i++ ) {
blobResume.data[k] = out[i];
k++;
}
}
}
blobResume.length= k;
}

--------------------------------------------------------------------------------
回页首
数据的转换效率和优化建议

在 IBM P570 数据库服务器上运行,该程序的运行效率非常高,先后进行了几个数量级的测试,最终平均测试的转换效率为:每 1 万笔数据记录,转换的效率 55 秒,即 182 条 / 秒。值得注意的是,整个转换过程占用 CPU 的量并不特别大,主要的性能瓶颈在磁盘阵列中。

以后可以进一步在以下方面进行调优,确保程序转换的效率更高:

1)采用多进程调用的方式,以获得更高的并发数量;

2)采用每 10 次或者 100 次提交事务的方式,减少访问磁盘的次数;

3)将 CLOB 和 BLOB 分别放置在不同的表空间上,并且将表空间分布在在多个磁盘上,获得最佳的磁盘访问速度。

c语言关于从数据库读取数据写文件

#include<stdio.h>
execsqlincludesqlca;

intmain(){
execsqlbegindeclaresection;
charuserpasswd[30]="openlab/123456";
struct{
intid;
charname[30];
doublesalary;
}emp;
execsqlenddeclaresection;
execsqlconnect:userpasswd;

selectid,first_name,salaryfrom
s_emporderbysalary;
execsqlopenempcursor;
;
for(;;){
execsqlfetchempcursorinto:emp;
printf("%d:%s:%lf ",emp.id,emp.name,
emp.salary);
}
execsqlcloseempcursor;
execsqlcommitworkrelease;
}

把数据存到结构体里。

㈢ C语言环境下如何使用动态SQL

你真是牛人呀。数据库类型那么多,有oraclemysqlDB2SQLSQLsevera。你使用的那种。

相对于来说我使用oracle多。

给你一个pro*c的操作实例吧

/ 定义符号常数
#define USERNAME "SCOTT"
#define PASSWORD "x"
#include <stdio.h>
// 说明SQLCA和ORACA
EXEC SQL INCLUDE SQLCA;
EXEC SQL INCLUDE ORACA;
// 启用ORACLE通讯区:ORACA=YES,使它能被使用
EXEC ORACLE OPTION (ORACA=YES);
// 说明SQL变量
EXEC SQL BEGIN DECLARE SECTION;
char* username=USERNAME;
char* password=PASSWORD;
VARCHAR sqlstmt[80];
int emp_number;
VARCHAR emp_name[15];
VARCHAR job[50],job1[50],job2[50];
float salary;
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL WHENEVER SQLERROR GOTO sqlerror;
// 发生错误时,保存SQL语句至ORACA
oraca.orastxtf=ORASTFERR;
// 登录到ORACLE
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf("/nConnect to ORACLE./n");
// 构造动态SQL语句
sqlstmt.len=sprintf(sqlstmt.arr,"INSERT INTO EMP(EMPNO,ENAME,JOB,SAL)VALUES(:V1,:V2,:V3,:V4)");
// 显示SQL语句
puts(sqlstmt.arr);
// 用PREPARE语句分析当前的动态INSERT语句,语句名是S
EXEC SQL PREPARE S FROM :sqlstmt;
// 循环插表
for(;;)
{
printf("/nEnter employee number:");
scanf("%d",&emp_number);
if(emp_number==0)break;
printf("/nEnter employee name:");
scanf("%s",&emp_name.arr);
emp_name.len=strlen(emp_name.arr);
printf("/nEnter employee job:");
scanf("%s",&job.arr);
job.len=strlen(job.arr);
salary = 0; // With VC6, Missing this line will cause C Run-Time Error R6002.
printf("/nEnter salary:");
scanf("%f",&salary);
EXEC SQL EXECUTE S USING :emp_number,:emp_name,:job,:salary;
}

// 提交事务,退出ORACLE
EXEC SQL COMMIT RELEASE;
printf("/nHave a good day!/n");
exit(0);
sqlerror:
// 打印错误信息
printf("/n%.*s/n",sqlca.sqlerrm.sqlerrml,sqlca.sqlerrm.sqlerrmc);
// 打印出错SQL语句
printf("/n/"%.*s.../"/n",oraca.orastxt.orastxtl,oraca.orastxt.orastxtc);
// 打印出错SQL语句所在行号及所在文件名
printf("on line %d of %.*s/n/n",oraca.oraslnr,
oraca.orasfnm.orasfnml,oraca.orasfnm.orasfnmc);

// 回滚事务,退出ORACLE
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL ROLLBACK RELEASE;
exit(1);
}

㈣ linux下c++远程连接oracle数据库

你说的复合结构应该是结构体吧。若是举个例子给你看看

/*
* sample2.pc
*
* This program connects to ORACLE, declares and opens a cursor,
* fetches the names, salaries, and commissions of all
* salespeople, displays the results, then closes the cursor.
*/
#include <stdio.h>
#include <sqlca.h>
#define UNAME_LEN 20
#define PWD_LEN 40
/*
* Use the precompiler typedef’ing capability to create
* null-terminated strings for the authentication host
* variables. (This isn’t really necessary--plain char *’s
* does work as well. This is just for illustration.)
*/
typedef char asciiz[PWD_LEN];
EXEC SQL TYPE asciiz IS STRING(PWD_LEN) REFERENCE;
asciiz username;
asciiz password;
struct emp_info
{
asciiz emp_name;
float salary;
float commission;
};
/* Declare function to handle unrecoverable errors. */
void sql_error();
main()
{
struct emp_info *emp_rec_ptr;
/* Allocate memory for emp_info struct. */
if ((emp_rec_ptr =
(struct emp_info *) malloc(sizeof(struct emp_info))) == 0)
{
fprintf(stderr, "Memory allocation error. ");
exit(1);
}
/* Connect to ORACLE. */
strcpy(username, "SCOTT");
strcpy(password, "TIGER");
EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--");
EXEC SQL CONNECT :username IDENTIFIED BY :password;
printf(" Connected to ORACLE as user: %s ", username);
/* Declare the cursor. All static SQL explicit cursors
* contain SELECT commands. ’salespeople’ is a SQL identifier,
* not a (C) host variable.
*/
EXEC SQL DECLARE salespeople CURSOR FOR
SELECT ENAME, SAL, COMM FROM EMP WHERE JOB LIKE ’SALES%’;
/* Open the cursor. */
EXEC SQL OPEN salespeople;
/* Get ready to print results. */
printf(" The company’s salespeople are-- ");
printf("Salesperson Salary Commission ");
printf("----------- ------ ---------- ");
/* Loop, fetching all salesperson’s statistics.
* Cause the program to break the loop when no more
* data can be retrieved on the cursor.
*/
EXEC SQL WHENEVER NOT FOUND DO break;
for (;;)
{
EXEC SQL FETCH salespeople INTO :emp_rec_ptr;
printf("%-11s%9.2f%13.2f ", emp_rec_ptr->emp_name,emp_rec_ptr->salary, emp_rec_ptr->commission);
}
/* Close the cursor. */
EXEC SQL CLOSE salespeople;
printf(" Arrivederci. ");
EXEC SQL COMMIT WORK RELEASE;
exit(0);
}
void sql_error(msg) char *msg;
{
char err_msg[512];
int buf_len, msg_len;
EXEC SQL WHENEVER SQLERROR CONTINUE;
printf(" %s ", msg);
/* Call sqlglm() to get the complete text of the
* error message.
*/
buf_len = sizeof (err_msg);
sqlglm(err_msg, &buf_len, &msg_len);
printf("%.*s ", msg_len, err_msg);
EXEC SQL ROLLBACK RELEASE;
exit(1);
}

㈤ C语言db2嵌入式SQL编程,编译问题 undefined reference to `sqlastrt'

1、要有类似的定义:
……
EXEC SQL INCLUDE SQLDA; /* or #include <sqlda.h> */
2、编译环境要有db2的权限和sqllib的路径
3、我已经上传了一份相关的文档,预计明后天审核通过就可以看到了
《DB2开发基础》
http://passport..com/?business&aid=6&un=chinacmouse#7

补充一个程序:
#include <time.h>
#include "stdio.h"

EXEC SQL INCLUDE SQLCA;

int main()
{
int i=0;
struct tm *pt;
time_t t1;
t1 = time(NULL);
pt = localtime(&t1);
printf("%4d%02d%02d", pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday);
printf("%02d:%02d:%02d\n",pt->tm_hour,pt->tm_min,pt->tm_sec);

EXEC SQL CONNECT TO db;
i=0;
while (i<3000)
{
int j=0;
while (j<1000)
{
EXEC SQL update cc.fund set cc_code='095' where cc_no='0950031359';
j++;
}
i++;
}
EXEC SQL COMMIT;
t1 = time(NULL);
pt = localtime(&t1);
printf("%4d%02d%02d", pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday);
printf("%02d:%02d:%02d\n",pt->tm_hour,pt->tm_min,pt->tm_sec);
EXEC SQL CONNECT RESET;
return 1;
}

编译脚本
db2 prep testdb.sqc target cplusplus bindfile using testdb.bnd package using testdb
db2 bind testdb.bnd
db2 grant execute on package testdb to public
gcc -I/app/db2inst1/sqllib/include -I./ -c -g testdb.C
gcc -L/app/db2inst1/sqllib/lib -ldb2 -L/usr/lib -lm -o testdb testdb.o