(8.0.31)
https://qiita.com/Ki4mTaria/items/778ff9186bb4958bb667
OS: CentOS 7
yum -y install gcc mysql-devel
vim a.c
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
int main(void)
{
MYSQL *conn = NULL;
MYSQL_RES *res = NULL;
MYSQL_ROW row;
char *host = "localhost";
char *user = "root";
char *password = "password";
char *dbname = "test";
int port = 3306;
conn = mysql_init(NULL);
if( !mysql_real_connect(conn,host,user,password,dbname,port,NULL,0) ){
exit(1);
}
if( mysql_query( conn , "select * from tab1" ) ){
mysql_close(conn);
exit(1);
}
res = mysql_use_result(conn);
while( (row = mysql_fetch_row(res) ) != NULL ){
printf( "%s : %s\n" , row[0] , row[1] );
}
mysql_free_result(res);
mysql_close(conn);
return 0;
}
---------------------
gcc -Wall -L/usr/lib64/mysql -lmysqlclient -o a a.c
./a
(19c)
https://qiita.com/khale/items/b8abac13d46985322271
OS: CentOS 7
vim a.pc
---------------------
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAX 100
EXEC SQL BEGIN DECLARE SECTION;
char username[30] = "test";
char password[30] = "test";
char dbname[30] = "pdb1";
char conn_string[30] = "mmm065:1521/pdb1.example.com";
char h_col1[MAX][10];
char h_col2[MAX][10];
EXEC SQL END DECLARE SECTION;
EXEC SQL INCLUDE sqlca.h;
int main(int argc, char *argv[])
{
int i;
EXEC SQL WHENEVER SQLERROR GOTO errorpt;
EXEC SQL CONNECT :username IDENTIFIED BY :password AT :dbname using :conn_string;
EXEC SQL AT :dbname DECLARE cursor CURSOR FOR SELECT COL1,COL2 FROM TAB1;
EXEC SQL OPEN cursor;
EXEC SQL FETCH cursor INTO :h_col1,:h_col2;
for(i=0;i<MAX;i++){
if( strlen(h_col1[i]) == 0 ){
printf("\n");
break;
}else{
printf("%s,%s\n",h_col1[i],h_col2[i]);
}
}
EXEC SQL CLOSE cursor;
return 0;
errorpt:
printf("\n\n%-79s \n",sqlca.sqlerrm.sqlerrmc);
EXEC SQL WHENEVER SQLERROR CONTINUE;
}
---------------------
proc iname=a.pc oname=a.c sqlcheck=full
gcc -I$ORACLE_HOME/precomp/public -L$ORACLE_HOME/lib -lclntsh -o a a.c
./a
(15)
https://qiita.com/tom-sato/items/aefc32addb2a1036a2b9
OS: Rocky Linux 8
dnf -y install gcc postgresql15-devel
vim a.c
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
int main(void)
{
const char *conninfo;
PGconn *conn;
PGresult *res;
int i;
conninfo = "host=localhost user=postgres password=postgres dbname=test port=5432";
conn = PQconnectdb(conninfo);
if( PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "%s", PQerrorMessage(conn) );
exit(1);
}
res = PQexec(conn, "select * from tab1");
if( PQresultStatus(res) != PGRES_TUPLES_OK )
{
fprintf( stderr, "%s", PQerrorMessage(conn) );
exit(1);
}
for( i = 0; i < PQntuples(res); i++ )
{
printf( "%s : %s\n", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1) );
}
PQclear(res);
PQfinish(conn);
return 0;
}
}
---------------------
gcc -Wall -I/usr/pgsql-15/include -L/usr/pgsql-15/lib -lpq -o a a.c
./a
(2019)
https://kledgeb.blogspot.com/2017/03/sql-server-10-microsof-sql-serverodbc.html
https://learn.microsoft.com/ja-jp/sql/odbc/reference/develop-app/using-sqlbindcol?view=sql-server-ver16
OS: CentOS 7
yum update -y
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
yum remove unixODBC-utf16 unixODBC-utf16-devel
ACCEPT_EULA=Y yum install msodbcsql17
yum install unixODBC-devel gcc
ls /usr/lib64/libodbc.so
odbcinst -j
cat /etc/odbcinst.ini
[sqlserver]
Driver = ODBC Driver 17 for SQL Server
Description = Microsoft SQL Server
Server = 10.1.1.61
Port = 1433
Database = test
isql sqlserver sa 'password' -v
vim a.c
---------------------
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
#include <string.h>
#define RESULT_LEN 256
#define CHECK_ERROR(e, s, h, t) ({\
if (e!=SQL_SUCCESS && e != SQL_SUCCESS_WITH_INFO) {extract_error(s, h, t); goto errorpt;} \
})
void extract_error(char *fn, SQLHANDLE handle, SQLSMALLINT type)
{
SQLINTEGER i = 0;
SQLINTEGER NativeError;
SQLCHAR SQLState[ 7 ];
SQLCHAR MessageText[256];
SQLSMALLINT TextLength;
SQLRETURN ret;
fprintf(stderr, "\nThe driver reported the following error %s\n", fn);
do
{
ret = SQLGetDiagRec(type, handle, ++i, SQLState, &NativeError, MessageText, sizeof(MessageText), &TextLength);
if (SQL_SUCCEEDED(ret) ) {
printf("%s:%ld:%ld:%s\n", SQLState, (long) i, (long) NativeError, MessageText);
}
}
while( ret == SQL_SUCCESS );
}
int main () {
SQLHENV henv = SQL_NULL_HENV; // Environment
SQLHDBC hdbc = SQL_NULL_HDBC; // Connection handle
SQLHSTMT hstmt = SQL_NULL_HSTMT; // Statement handle
SQLRETURN retcode;
SQLCHAR strCOL1[RESULT_LEN];
SQLCHAR strCOL2[RESULT_LEN];
SQLCHAR outstr[1024];
SQLSMALLINT outstrlen;
int i=0;
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_ENV)", henv, SQL_HANDLE_ENV);
retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER*)SQL_OV_ODBC3, 0);
CHECK_ERROR(retcode, "SQLSetEnvAttr(SQL_ATTR_ODBC_VERSION)", henv, SQL_HANDLE_ENV);
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_DBC)", hdbc, SQL_HANDLE_DBC);
SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);
CHECK_ERROR(retcode, "SQLSetConnectAttr(SQL_LOGIN_TIMEOUT)", hdbc, SQL_HANDLE_DBC);
retcode = SQLDriverConnect(hdbc, NULL, "Driver=ODBC Driver 17 for SQL Server;Server=10.1.1.61;Uid=sa;Pwd=password;database=test", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_NOPROMPT);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
CHECK_ERROR(retcode, "SQLAllocHandle(SQL_HANDLE_STMT)", hstmt, SQL_HANDLE_STMT);
retcode = SQLExecDirect(hstmt, (SQLCHAR*) "SELECT COL1,COL2 FROM TAB1", SQL_NTS);
CHECK_ERROR(retcode, "SQLExecDirect()", hstmt, SQL_HANDLE_STMT);
retcode = SQLBindCol(hstmt, 1, SQL_C_CHAR, &strCOL1, RESULT_LEN, 0);
retcode = SQLBindCol(hstmt, 2, SQL_C_CHAR, &strCOL2, RESULT_LEN, 0);
for (i=0; ; i++) {
retcode = SQLFetch(hstmt);
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) {
printf("%s,%s\n", strCOL1,strCOL2);
} else {
if (retcode != SQL_NO_DATA) {
CHECK_ERROR(retcode, "SQLFetch()", hstmt, SQL_HANDLE_STMT);
} else {
break;
}
}
}
errorpt:
if (hstmt != SQL_NULL_HSTMT) {
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
}
if (hdbc != SQL_NULL_HDBC) {
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
if (henv != SQL_NULL_HENV) {
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
return 0;
}
---------------------
gcc -lodbc -o a a.c
./a