Sample Programs

Example of Sample

Let's write examples for connection by using CLI. The name of sample file is sample1_connect.c.

MACHBASE_PORT_NO should be equal to the value of PORT_NO in $MACHBASE_HOME/conf/machbase.conf file.

#include <stdio.h>
#include <stdlib.h>
#include <machbase_sqlcli.h>

#define MACHBASE_PORT_NO 5656

SQLHENV gEnv;
SQLHDBC gCon;
SQLHSTMT gStmt;

void connectDB()
{
char connStr[1024];

SQLINTEGER errNo;
SQLSMALLINT msgLength;
SQLCHAR errMsg[1024];

if (SQL_ERROR == SQLAllocEnv(&gEnv)) {
printf("SQLAllocEnv error!!\n");
exit(1);
}

if (SQL_ERROR == SQLAllocConnect(gEnv, &gCon)) {
printf("SQLAllocConnect error!!\n");
SQLFreeEnv(gEnv);
exit(1);
}

sprintf(connStr,"DSN=127.0.0.1;UID=SYS;PWD=MANAGER;CONNTYPE=1;PORT_NO=%d", MACHBASE_PORT_NO);

if (SQL_ERROR == SQLDriverConnect( gCon, NULL,
(SQLCHAR *)connStr,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT ))
{
printf("connection error\n");

if (SQL_SUCCESS == SQLError ( gEnv, gCon, NULL, NULL, &errNo,
errMsg, 1024, &msgLength ))
{
printf("mach-%d : %s\n", errNo, errMsg);
}
SQLFreeEnv(gEnv);
exit(1);
}

printf("connected ... \n");

}

void disconnectDB()
{
SQLINTEGER errNo;
SQLSMALLINT msgLength;
SQLCHAR errMsg[1024];


if (SQL_ERROR == SQLDisconnect(gCon)) {
printf("disconnect error\n");


if( SQL_SUCCESS == SQLError( gEnv, gCon, NULL, NULL, &errNo,
errMsg, 1024, &msgLength ))
{
printf("mach-%d : %s\n", errNo, errMsg);
}
}


SQLFreeConnect(gCon);
SQLFreeEnv(gEnv);
}


int main()
{

connectDB();


disconnectDB();


return 0;
}

Register and compile sample1_connect.c in the makefile, then the result will be shown as follows.

[mach@localhost cli]$ make

[ilfux@localhost cli]$ ./sample1_connect
connected ...
[mach@localhost cli]$

Example of Data Input and Output

The example source below shows how to create a table. And it randomly generates values of data and inserts the data by using INSERT statement. Then, select data by using SELECT statement and display it. By looking at the sample below, you can know how to set it for each type when you enter and check data.

The name of sample file is "sample2_insert.c".

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <machbase_sqlcli.h>

#define MACHBASE_PORT_NO 5656

SQLHENV gEnv;
SQLHDBC gCon;
SQLHSTMT gStmt;
SQLCHAR gErrorState[6];

void connectDB()
{
char connStr[1024];

SQLINTEGER errNo;
SQLSMALLINT msgLength;
SQLCHAR errMsg[1024];

if (SQL_ERROR == SQLAllocEnv(&gEnv)) {
printf("SQLAllocEnv error!!\n");
exit(1);
}

if (SQL_ERROR == SQLAllocConnect(gEnv, &gCon)) {
printf("SQLAllocConnect error!!\n");
SQLFreeEnv(gEnv);
exit(1);
}

sprintf(connStr,"DSN=127.0.0.1;UID=SYS;PWD=MANAGER;CONNTYPE=1;PORT_NO=%d", MACHBASE_PORT_NO);

if (SQL_ERROR == SQLDriverConnect( gCon, NULL,
(SQLCHAR *)connStr,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT ))
{
printf("connection error\n");

if (SQL_SUCCESS == SQLError ( gEnv, gCon, NULL, NULL, &errNo,
errMsg, 1024, &msgLength ))
{
printf(" mach-%d : %s\n", errNo, errMsg);
}
SQLFreeEnv(gEnv);
exit(1);
}

printf("connected ... \n");

}

void disconnectDB()
{
SQLINTEGER errNo;
SQLSMALLINT msgLength;
SQLCHAR errMsg[1024];

if (SQL_ERROR == SQLDisconnect(gCon)) {
printf("disconnect error\n");

if( SQL_SUCCESS == SQLError( gEnv, gCon, NULL, NULL, &errNo,
errMsg, 1024, &msgLength ))
{
printf(" mach-%d : %s\n", errNo, errMsg);
}
}

SQLFreeConnect(gCon);
SQLFreeEnv(gEnv);
}

void outError(const char *aMsg, SQLHSTMT stmt)
{
SQLINTEGER errNo;
SQLSMALLINT msgLength;
SQLCHAR errMsg[1024];

printf("ERROR : (%s)\n", aMsg);

if (SQL_SUCCESS == SQLError( gEnv, gCon, stmt, NULL, &errNo,
errMsg, 1024, &msgLength ))
{
printf(" mach-%d : %s\n", errNo, errMsg);
}
exit(-1);
}

void executeDirectSQL(const char *aSQL, int aErrIgnore)
{
SQLHSTMT stmt;

if (SQLAllocStmt(gCon, &stmt) == SQL_ERROR)
{
if (aErrIgnore != 0) return;
outError("AllocStmt error", stmt);
}

if (SQLExecDirect(stmt, (SQLCHAR *)aSQL, SQL_NTS) == SQL_ERROR)
{
if (aErrIgnore != 0) return;
printf("sql_exec_direct error[%s] \n", aSQL);
outError("sql_exec_direct error", stmt);
}

if (SQL_ERROR == SQLFreeStmt(stmt, SQL_DROP))
{
if (aErrIgnore != 0) return;
outError("FreeStmt Error", stmt);
}
}

void prepareExecuteSQL(const char *aSQL)
{
SQLHSTMT stmt;

if (SQLAllocStmt(gCon, &stmt) == SQL_ERROR)
{
outError("AllocStmt error", stmt);
}

if (SQLPrepare(stmt, (SQLCHAR *)aSQL, SQL_NTS) == SQL_ERROR)
{
printf("Prepare error[%s]\n", aSQL);
outError("Prepare error", stmt);
}

if (SQLExecute(stmt) == SQL_ERROR)
{
outError("prepared execute error", stmt);
}

if (SQL_ERROR == SQLFreeStmt(stmt, SQL_DROP))
{
outError("FreeStmt Error", stmt);
}
}

void createTable()
{
executeDirectSQL("DROP TABLE CLI_SAMPLE1", 1);

executeDirectSQL("CREATE TABLE CLI_SAMPLE1(seq short, score integer, total long, percentage float, ratio double, id varchar(10), srcip ipv4, dstip ipv6, reg_date datetime, textlog text, image binary)", 0);
}

void selectTable()
{
SQLHSTMT stmt;
const char *aSQL = "SELECT seq, score, total, percentage, ratio, id, srcip, dstip, reg_date, textlog, image FROM CLI_SAMPLE1";
int i=0;
SQLLEN Len = 0;

short seq;
int score;
long total;
float percentage;
double ratio;
char id [11];
char srcip[16];
char dstip[40];
SQL_TIMESTAMP_STRUCT regdate;
char log [1024];
char image[1024];

if (SQLAllocStmt(gCon, &stmt) == SQL_ERROR) {
outError("AllocStmt Error", stmt);
}

if (SQLPrepare(stmt, (SQLCHAR *)aSQL, SQL_NTS) == SQL_ERROR) {
printf("Prepare error[%s] \n", aSQL);
outError("Prepare error", stmt);
}

if (SQLExecute(stmt) == SQL_ERROR) {
outError("prepared execute error", stmt);
}

SQLBindCol(stmt, 1, SQL_C_SHORT, &seq, 0, &Len);
SQLBindCol(stmt, 2, SQL_C_LONG, &score, 0, &Len);
SQLBindCol(stmt, 3, SQL_C_BIGINT, &total, 0, &Len);
SQLBindCol(stmt, 4, SQL_C_FLOAT, &percentage, 0, &Len);
SQLBindCol(stmt, 5, SQL_C_DOUBLE, &ratio, 0, &Len);
SQLBindCol(stmt, 6, SQL_C_CHAR, id, sizeof(id), &Len);
SQLBindCol(stmt, 7, SQL_C_CHAR, srcip, sizeof(srcip), &Len);
SQLBindCol(stmt, 8, SQL_C_CHAR, dstip, sizeof(dstip), &Len);
SQLBindCol(stmt, 9, SQL_C_TYPE_TIMESTAMP, &regdate, 0, &Len);
SQLBindCol(stmt, 10, SQL_C_CHAR, log, sizeof(log), &Len);
SQLBindCol(stmt, 11, SQL_C_CHAR, image, sizeof(image), &Len);

while (SQLFetch(stmt) == SQL_SUCCESS)
{
printf("===== %d ========\n", i++);
printf("seq = %d", seq);
printf(", score = %d", score);
printf(", total = %ld", total);
printf(", percentage = %.2f", percentage);
printf(", ratio = %g", ratio);
printf(", id = %s", id);
printf(", srcip = %s", srcip);
printf(", dstip = %s", dstip);
printf(", regdate = %d-%02d-%02d %02d:%02d:%02d",
regdate.year, regdate.month, regdate.day,
regdate.hour, regdate.minute, regdate.second);
printf(", log = %s", log);
printf(", image = %s\n", image);
}

if (SQL_ERROR == SQLFreeStmt(stmt, SQL_DROP))
{
outError("FreeStmt eror", stmt);
}
}

void directInsert()
{
int i;
char query[2 * 1024];

short seq;
int score;
long total;
float percentage;
double ratio;
char id [11];
char srcip [16];
char dstip [40];
char reg_date [40];
char log [1024];
char image [1024];

for(i=1; i<10; i++)
{
seq = i;
score = i+i;
total = (seq + score) * 10000;
percentage = (float)score/total;
ratio = (double)seq/total;
sprintf(id, "id-%d", i);
sprintf(srcip, "192.168.0.%d", i);
sprintf(dstip, "2001:0DB8:0000:0000:0000:0000:1428:%04d", i);
sprintf(reg_date, "2015-03-31 15:26:%02d", i);
sprintf(log, "text log-%d", i);
sprintf(image, "binary image-%d", i);

memset(query, 0x00, sizeof(query));

sprintf(query, "INSERT INTO CLI_SAMPLE1 VALUES(%d, %d, %ld, %f, %f, '%s', '%s', '%s',TO_DATE('%s','YYYY-MM-DD HH24:MI:SS'),'%s','%s')",
seq, score, total, percentage, ratio, id, srcip, dstip, reg_date, log, image);

prepareExecuteSQL(query);

printf("%d record inserted\n", i);
}
}

int main()
{

connectDB();

createTable();

directInsert();

selectTable();

disconnectDB();

return 0;
}

Register and compile "sample2_insert.c" to the makefile, then the result will be shown as follow.

[mach@localhost cli]$ make

[ilfux@localhost cli]$ ./sample2_insert

connected ...
1 record inserted
2 record inserted
3 record inserted
4 record inserted
5 record inserted
6 record inserted
7 record inserted
8 record inserted
9 record inserted
===== 0 ========
seq = 9, score = 18, total = 270000, percentage = 0.00, ratio = 3.3e-05, id = id-9, srcip = 192.168.0.9, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0009, regdate = 2015-03-31 15:26:09, log = text log-9, image = 62696E61727920696D6167652D39
===== 1 ========
seq = 8, score = 16, total = 240000, percentage = 0.00, ratio = 3.3e-05, id = id-8, srcip = 192.168.0.8, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0008, regdate = 2015-03-31 15:26:08, log = text log-8, image = 62696E61727920696D6167652D38
===== 2 ========
seq = 7, score = 14, total = 210000, percentage = 0.00, ratio = 3.3e-05, id = id-7, srcip = 192.168.0.7, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0007, regdate = 2015-03-31 15:26:07, log = text log-7, image = 62696E61727920696D6167652D37
===== 3 ========
seq = 6, score = 12, total = 180000, percentage = 0.00, ratio = 3.3e-05, id = id-6, srcip = 192.168.0.6, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0006, regdate = 2015-03-31 15:26:06, log = text log-6, image = 62696E61727920696D6167652D36
===== 4 ========
seq = 5, score = 10, total = 150000, percentage = 0.00, ratio = 3.3e-05, id = id-5, srcip = 192.168.0.5, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0005, regdate = 2015-03-31 15:26:05, log = text log-5, image = 62696E61727920696D6167652D35
===== 5 ========
seq = 4, score = 8, total = 120000, percentage = 0.00, ratio = 3.3e-05, id = id-4, srcip = 192.168.0.4, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0004, regdate = 2015-03-31 15:26:04, log = text log-4, image = 62696E61727920696D6167652D34
===== 6 ========
seq = 3, score = 6, total = 90000, percentage = 0.00, ratio = 3.3e-05, id = id-3, srcip = 192.168.0.3, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0003, regdate = 2015-03-31 15:26:03, log = text log-3, image = 62696E61727920696D6167652D33
===== 7 ========
seq = 2, score = 4, total = 60000, percentage = 0.00, ratio = 3.3e-05, id = id-2, srcip = 192.168.0.2, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0002, regdate = 2015-03-31 15:26:02, log = text log-2, image = 62696E61727920696D6167652D32
===== 8 ========
seq = 1, score = 2, total = 30000, percentage = 0.00, ratio = 3.3e-05, id = id-1, srcip = 192.168.0.1, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0001, regdate = 2015-03-31 15:26:01, log = text log-1, image = 62696E61727920696D6167652D31
[mach@localhost cli]$

Example of Prepare and Execute Statements

Create an example by binding data and then, insert it. You can enter data values by binding them in Machbase. When you use this method, you have to specify the types of data values and the value of length as well if it's a long string type. You can know how to bind data based on types through the example below.

The file name is "sample3_prepare.c".

/******************************************************************************
* Copyright of this product 2013-2023,
* Machbase Inc. or its subsidiaries.
* All Rights reserved.
******************************************************************************/

/* $Id:$ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <machbase_sqlcli.h>
#include <time.h>

#define MACHBASE_PORT_NO 5656

SQLHENV gEnv;
SQLHDBC gCon;
SQLHSTMT gStmt;
SQLCHAR gErrorState[6];

void connectDB()
{
char sConnStr[1024];

SQLINTEGER sErrorNo;
SQLSMALLINT sMsgLength;
SQLCHAR sErrorMsg[1024];

if (SQL_ERROR == SQLAllocEnv(&gEnv)) {
printf("SQLAllocEnv error!!\n");
exit(1);
}

if (SQL_ERROR == SQLAllocConnect(gEnv, &gCon)) {
printf("SQLAllocConnect error!!\n");
SQLFreeEnv(gEnv);
exit(1);
}

sprintf(sConnStr,"DSN=127.0.0.1;UID=SYS;PWD=MANAGER;CONNTYPE=1;PORT_NO=%d", MACHBASE_PORT_NO);

if (SQL_ERROR == SQLDriverConnect( gCon, NULL,
(SQLCHAR *)sConnStr,
SQL_NTS,
NULL, 0, NULL,
SQL_DRIVER_NOPROMPT ))
{
printf("connection error\n");

if (SQL_SUCCESS == SQLError ( gEnv, gCon, NULL, NULL, &sErrorNo,
sErrorMsg, 1024, &sMsgLength ))
{
printf(" mach-%d : %s\n", sErrorNo, sErrorMsg);
}
SQLFreeEnv(gEnv);
exit(1);
}

printf("connected ... \n");

}

void disconnectDB()
{
SQLINTEGER sErrorNo;
SQLSMALLINT sMsgLength;
SQLCHAR sErrorMsg[1024];

if (SQL_ERROR == SQLDisconnect(gCon)) {
printf("disconnect error\n");

if( SQL_SUCCESS == SQLError( gEnv, gCon, NULL, NULL, &sErrorNo,
sErrorMsg, 1024, &sMsgLength ))
{
printf(" mach-%d : %s\n", sErrorNo, sErrorMsg);
}
}

SQLFreeConnect(gCon);
SQLFreeEnv(gEnv);
}

void outError(const char *aMsg, SQLHSTMT aStmt)
{
SQLINTEGER sErrorNo;
SQLSMALLINT sMsgLength;
SQLCHAR sErrorMsg[1024];

printf("ERROR : (%s)\n", aMsg);

if (SQL_SUCCESS == SQLError( gEnv, gCon, aStmt, NULL, &sErrorNo,
sErrorMsg, 1024, &sMsgLength ))
{
printf(" mach-%d : %s\n", sErrorNo, sErrorMsg);
}
exit(-1);
}

void executeDirectSQL(const char *aSQL, int aErrIgnore)
{
SQLHSTMT sStmt;

if (SQLAllocStmt(gCon, &sStmt) == SQL_ERROR)
{
if (aErrIgnore != 0) return;
outError("AllocStmt error", sStmt);
}

if (SQLExecDirect(sStmt, (SQLCHAR *)aSQL, SQL_NTS) == SQL_ERROR)
{
if (aErrIgnore != 0) return;
printf("sql_exec_direct error[%s] \n", aSQL);
outError("sql_exec_direct error", sStmt);
}

if (SQL_ERROR == SQLFreeStmt(sStmt, SQL_DROP))
{
if (aErrIgnore != 0) return;
outError("FreeStmt Error", sStmt);
}
}

void createTable()
{
executeDirectSQL("DROP TABLE CLI_SAMPLE", 1);
executeDirectSQL("CREATE TABLE CLI_SAMPLE(seq short, score integer, total long, percentage float, ratio double, id varchar(10), srcip ipv4, dstip ipv6, reg_date datetime, tlog text, image binary)", 0);
}

void selectTable()
{
SQLHSTMT sStmt;
const char *aSQL = "SELECT seq, score, total, percentage, ratio, id, srcip, dstip, reg_date, tlog, image FROM CLI_SAMPLE";

int i=0;
short sSeq;
int sScore;
long sTotal;
float sPercentage;
double sRatio;
char sId [20];
char sSrcIp[20];
char sDstIp[50];
SQL_TIMESTAMP_STRUCT sRegDate;
char sLog [1024];
char sImage[1024];
SQL_LEN sLen;

if (SQLAllocStmt(gCon, &sStmt) == SQL_ERROR) {
outError("AllocStmt Error", sStmt);
}

if (SQLPrepare(sStmt, (SQLCHAR *)aSQL, SQL_NTS) == SQL_ERROR) {
printf("Prepare error[%s] \n", aSQL);
outError("Prepare error", sStmt);
}

if (SQLExecute(sStmt) == SQL_ERROR) {
outError("prepared execute error", sStmt);
}

SQLBindCol(sStmt, 1, SQL_C_SSHORT, &sSeq, 0, &sLen);
SQLBindCol(sStmt, 2, SQL_C_SLONG, &sScore, 0, &sLen);
SQLBindCol(sStmt, 3, SQL_C_SBIGINT, &sTotal, 0, &sLen);
SQLBindCol(sStmt, 4, SQL_C_FLOAT, &sPercentage, 0, &sLen);
SQLBindCol(sStmt, 5, SQL_C_DOUBLE, &sRatio, 0, &sLen);
SQLBindCol(sStmt, 6, SQL_C_CHAR, sId, sizeof(sId), &sLen);
SQLBindCol(sStmt, 7, SQL_C_CHAR, sSrcIp, sizeof(sSrcIp), &sLen);
SQLBindCol(sStmt, 8, SQL_C_CHAR, sDstIp, sizeof(sDstIp), &sLen);
SQLBindCol(sStmt, 9, SQL_C_TYPE_TIMESTAMP, &sRegDate, 0, &sLen);
SQLBindCol(sStmt, 10, SQL_C_CHAR, sLog, sizeof(sLog), &sLen);
SQLBindCol(sStmt, 11, SQL_C_CHAR, sImage, sizeof(sImage), &sLen);

while (SQLFetch(sStmt) == SQL_SUCCESS)
{
printf("===== %d ========\n", i++);
printf("seq = %d", sSeq);
printf(", score = %d", sScore);
printf(", total = %ld", sTotal);
printf(", percentage = %.2f", sPercentage);
printf(", ratio = %g", sRatio);
printf(", id = %s", sId);
printf(", srcip = %s", sSrcIp);
printf(", dstip = %s", sDstIp);
printf(", regdate = %d-%02d-%02d %02d:%02d:%02d",
sRegDate.year, sRegDate.month, sRegDate.day,
sRegDate.hour, sRegDate.minute, sRegDate.second);
printf(", log = %s", sLog);
printf(", image = %s\n", sImage);
}

if (SQL_ERROR == SQLFreeStmt(sStmt, SQL_DROP))
{
outError("FreeStmt eror", sStmt);
}
}

void prepareInsert()
{
SQLHSTMT sStmt;
int i;
short sSeq;
int sScore;
long sTotal;
float sPercentage;
double sRatio;
char sId [20];
char sSrcIp [20];
char sDstIp [50];
long reg_date;
char sLog [100];
char sImage [100];
int sLength[5];

const char *sSQL = "INSERT INTO CLI_SAMPLE VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";

if (SQLAllocStmt(gCon, &sStmt) == SQL_ERROR)
{
outError("AllocStmt error", sStmt);
}

if (SQLPrepare(sStmt, (SQLCHAR *)sSQL, SQL_NTS) == SQL_ERROR)
{
printf("Prepare error[%s]\n", sSQL);
outError("Prepare error", sStmt);
}

for(i=1; i<10; i++)
{
sSeq = i;
sScore = i+i;
sTotal = (sSeq + sScore) * 10000;
sPercentage = (float)(sScore+2)/sScore;
sRatio = (double)(sSeq+1)/sTotal;
sprintf(sId, "id-%d", i);
sprintf(sSrcIp, "192.168.0.%d", i);
sprintf(sDstIp, "2001:0DB8:0000:0000:0000:0000:1428:%04x", i);
reg_date = i*10000;
sprintf(sLog, "log-%d", i);
sprintf(sImage, "image-%d", i);

if (SQLBindParameter(sStmt,
1,
SQL_PARAM_INPUT,
SQL_C_SSHORT,
SQL_SMALLINT,
0,
0,
&sSeq,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 1", sStmt);
}

if (SQLBindParameter(sStmt,
2,
SQL_PARAM_INPUT,
SQL_C_SLONG,
SQL_INTEGER,
0,
0,
&sScore,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 2", sStmt);
}

if (SQLBindParameter(sStmt,
3,
SQL_PARAM_INPUT,
SQL_C_SBIGINT,
SQL_BIGINT,
0,
0,
&sTotal,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 3", sStmt);
}

if (SQLBindParameter(sStmt,
4,
SQL_PARAM_INPUT,
SQL_C_FLOAT,
SQL_FLOAT,
0,
0,
&sPercentage,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 4", sStmt);
}

if (SQLBindParameter(sStmt,
5,
SQL_PARAM_INPUT,
SQL_C_DOUBLE,
SQL_DOUBLE,
0,
0,
&sRatio,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 5", sStmt);
}

sLength[0] = strlen(sId);
if (SQLBindParameter(sStmt,
6,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
0,
0,
sId,
0,
(SQLLEN *)&sLength[0]) == SQL_ERROR)
{
outError("BindParameter error 6", sStmt);
}

sLength[1] = strlen(sSrcIp);
if (SQLBindParameter(sStmt,
7,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_IPV4,
0,
0,
sSrcIp,
0,
(SQLLEN *)&sLength[1]) == SQL_ERROR)
{
outError("BindParameter error 7", sStmt);
}

sLength[2] = strlen(sDstIp);
if (SQLBindParameter(sStmt,
8,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_IPV6,
0,
0,
sDstIp,
0,
(SQLLEN *)&sLength[2]) == SQL_ERROR)
{
outError("BindParameter error 8", sStmt);
}

if (SQLBindParameter(sStmt,
9,
SQL_PARAM_INPUT,
SQL_C_SBIGINT,
SQL_DATE,
0,
0,
&reg_date,
0,
NULL) == SQL_ERROR)
{
outError("BindParameter error 9", sStmt);
}

sLength[3] = strlen(sLog);
if (SQLBindParameter(sStmt,
10,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_VARCHAR,
0,
0,
sLog,
0,
(SQLLEN *)&sLength[3]) == SQL_ERROR)
{
outError("BindParameter error 10", sStmt);
}

sLength[4] = strlen(sImage);
if (SQLBindParameter(sStmt,
11,
SQL_PARAM_INPUT,
SQL_C_CHAR,
SQL_BINARY,
0,
0,
sImage,
0,
(SQLLEN *)&sLength[4]) == SQL_ERROR)
{
outError("BindParameter error 11", sStmt);
}

if( SQLExecute(sStmt) == SQL_ERROR) {
outError("prepare execute error", sStmt);
}

printf("%d prepared record inserted\n", i);

}

if (SQL_ERROR == SQLFreeStmt(sStmt, SQL_DROP)) {
outError("FreeStmt", sStmt);
}
}

int main()
{

connectDB();

createTable();

prepareInsert();

selectTable();

disconnectDB();

return 0;
}
​Register and compile "sample3_prepare.c" in the makefile, then the result will be shown as follows.

[mach@localhost cli]$ make

[ilfux@localhost cli]$ ./sample3_prepare

connected ...
1 prepared record inserted
2 prepared record inserted
3 prepared record inserted
4 prepared record inserted
5 prepared record inserted
6 prepared record inserted
7 prepared record inserted
8 prepared record inserted
9 prepared record inserted
===== 0 ========
seq = 9, score = 18, total = 270000, percentage = 1.11, ratio = 3.7037e-05, id = id-9, srcip = 192.168.0.9, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0009, regdate = 1970-01-01 09:00:00, log = log-9, image = 696D6167652D39
===== 1 ========
seq = 8, score = 16, total = 240000, percentage = 1.12, ratio = 3.75e-05, id = id-8, srcip = 192.168.0.8, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0008, regdate = 1970-01-01 09:00:00, log = log-8, image = 696D6167652D38
===== 2 ========
seq = 7, score = 14, total = 210000, percentage = 1.14, ratio = 3.80952e-05, id = id-7, srcip = 192.168.0.7, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0007, regdate = 1970-01-01 09:00:00, log = log-7, image = 696D6167652D37
===== 3 ========
seq = 6, score = 12, total = 180000, percentage = 1.17, ratio = 3.88889e-05, id = id-6, srcip = 192.168.0.6, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0006, regdate = 1970-01-01 09:00:00, log = log-6, image = 696D6167652D36
===== 4 ========
seq = 5, score = 10, total = 150000, percentage = 1.20, ratio = 4e-05, id = id-5, srcip = 192.168.0.5, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0005, regdate = 1970-01-01 09:00:00, log = log-5, image = 696D6167652D35
===== 5 ========
seq = 4, score = 8, total = 120000, percentage = 1.25, ratio = 4.16667e-05, id = id-4, srcip = 192.168.0.4, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0004, regdate = 1970-01-01 09:00:00, log = log-4, image = 696D6167652D34
===== 6 ========
seq = 3, score = 6, total = 90000, percentage = 1.33, ratio = 4.44444e-05, id = id-3, srcip = 192.168.0.3, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0003, regdate = 1970-01-01 09:00:00, log = log-3, image = 696D6167652D33
===== 7 ========
seq = 2, score = 4, total = 60000, percentage = 1.50, ratio = 5e-05, id = id-2, srcip = 192.168.0.2, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0002, regdate = 1970-01-01 09:00:00, log = log-2, image = 696D6167652D32
===== 8 ========
seq = 1, score = 2, total = 30000, percentage = 2.00, ratio = 6.66667e-05, id = id-1, srcip = 192.168.0.1, dstip = 2001:0DB8:0000:0000:0000:0000:1428:0001, regdate = 1970-01-01 09:00:00, log = log-1, image = 696D6167652D31
[mach@localhost cli]$

results matching ""

    No results matching ""