1.Mysql
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
int main(void) {
MYSQL *g_conn; // mysql 连接
const char *g_host_name = "10.111.14.10";
const char *g_user_name = "sinorail";
const char *g_password = "sinorail";
const char *g_db_name = "sinorail";
const unsigned int g_db_port = 3306;
// init the database connection
g_conn = mysql_init(NULL);
g_conn = mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0);
if(g_conn)
{
mysql_close(g_conn);
printf("连接成功\n");
}
else
{
mysql_close(g_conn);
printf("连接失败\n");
}
return 0;
}
编译
gcc test.c -o test -L/usr/lib64/mysql -lmysqlclient
2.sqlserver
安装 freetds
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sybfront.h>
#include<sybdb.h>
int mssqlIsConnect(char *serverIp,char *userName,char *pwd,char *sid,int port)
{
//初始化db-library
dbinit();
//连接数据库
LOGINREC *loginrec=dblogin();
DBSETLUSER(loginrec,userName);
DBSETLPWD(loginrec,pwd);
char szServer[32];
sprintf(szServer,"%s:%d",serverIp,port);
DBPROCESS *dbprocess=dbopen(loginrec,szServer);
if(dbprocess==FAIL){
//printf("Conect mssql fail\n");
//关闭数据库连接
dbclose(dbprocess);
return -1;
}else{
printf("Connect mssql success\n");
}
if(dbuse(dbprocess,sid)==FAIL){
//关闭数据库连接
dbclose(dbprocess);
//printf("Open database fail\n");
return -1;
}else{
//printf("Open database successn\n");
}
//关闭数据库连接
dbclose(dbprocess);
return 0;
}
int main(void)
{
char szUsername[32]= "sa";
char szPassword[32]= "sa";
char szDBName[32]= "fwkCeshi";
char szServer[32]= "192.168.1.59";
int port =1433;
mssqlIsConnect(szServer,szUsername,szPassword,szDBName,port);
printf("buf=%s\n",buf);
return 0;
}
编译
gcc test.c -o test -L/usr/local/freetds/lib -lsybdb -I/usr/local/freetds/include
3.oracle
使用oci
#include <stdio.h>
#include <stdlib.h>
#include "ocilib.h"
#include <stdio.h>
#include "oracleHelper.h"
void err_handler(OCI_Error *err)
{
printf("%s\n", OCI_ErrorGetString(err));
}
int oraIsConnect(char *serverIp,char *userName,char *pwd,char *sid,int port)
{
OCI_Connection * cn = NULL;
if ( !OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT) )
{
return -1;
}
//printf("init success\n");
char buf[100];
memset(buf,0,sizeof(buf));
sprintf(buf,"%s:%d/%s",serverIp,port,sid);
cn = OCI_ConnectionCreate(buf,userName, pwd, OCI_SESSION_DEFAULT); //OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
if ( cn != NULL )
{
//连接成功
//printf("%s\n", OCI_GetVersionServer(cn));
OCI_ConnectionFree(cn);
}
else
{
OCI_ConnectionFree(cn);
OCI_Cleanup();
return -1;
}
OCI_Cleanup();
return 0;
}
int oraGetExceRes(char *serverIp,char *userName,char *pwd,char *sid,int port,char *buf)
{
char sqlShowTableStr[50] = "SELECT TABLE_NAME FROM USER_TABLES";
char sqlGetPKNum[2048];
OCI_Statement *st;
OCI_Resultset *rs;
OCI_Statement *stPK;
OCI_Resultset *rsPK;
memset(buf,0,sizeof(buf));
OCI_Connection * cn = NULL;
if ( !OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT) )
{
return -1;
}
char dbbuf[100];
memset(dbbuf,0,sizeof(dbbuf));
sprintf(dbbuf,"%s:%d/%s",serverIp,port,sid);
cn = OCI_ConnectionCreate(dbbuf,userName, pwd, OCI_SESSION_DEFAULT); //OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
if ( cn == NULL )
{
OCI_ConnectionFree(cn);
OCI_Cleanup();
return -1;
}
st = OCI_StatementCreate(cn);
stPK = OCI_StatementCreate(cn);
OCI_Prepare(st, sqlShowTableStr);
OCI_Execute(st);
rs = OCI_GetResultset(st);
char tableName[256];
int pkNum;
char pkNumStr[10];
while (OCI_FetchNext(rs))
{
memset(tableName,0,sizeof(tableName));
sprintf(tableName,"%s",OCI_GetString(rs,1));
if(strcmp(tableName,"recordinfo1")!=0 && strcmp(tableName,"tableframe")!=0)
{
strcat(buf,tableName);
strcat(buf,":");
memset(sqlGetPKNum,0,sizeof(sqlGetPKNum));
sprintf(sqlGetPKNum,"select count(*) from user_cons_columns a, user_constraints b where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and a.table_name = '%s'",tableName);
//printf("sqlGetPKNum=%s\n",sqlGetPKNum);
OCI_ExecuteStmt(stPK, sqlGetPKNum);
rsPK = OCI_GetResultset(stPK);
OCI_FetchNext(rsPK);
memset(pkNumStr,0,sizeof(pkNumStr));
pkNum = OCI_GetInt(rsPK, 1);
sprintf(pkNumStr, "%d", pkNum);
strcat(buf,pkNumStr);
strcat(buf,";");
}
}
OCI_Commit(cn);
OCI_StatementFree(stPK);
OCI_StatementFree(st);
OCI_ConnectionFree(cn);
OCI_Cleanup();
return 0;
}
int main(void)
{
char szUsername[32]= "test";
char szPassword[32]= "123456";
char szDBName[32]= "orcl";
char szServer[32]= "192.168.1.59";
int port =1521;
int rc=0;
char buf[4096];
printf("test begain\n");
rc = IsConnect(szServer,szUsername,szPassword,szDBName,port);
printf(" IsConnect rc=%d\n",rc);
rc = GetExceRes(szServer,szUsername,szPassword,szDBName,port,buf);
printf("rc=%d\n",rc);
printf("GetExceRes buf=%s\n",buf);
return 0;
}
编译
gcc oracleHelper.c -locilib