本文目录一览:
用C语言如何对MySQL数据库进行操作
有时为了性能,我们会直接用C语言来开发相关的模块,尤其在我们的web应用中,虽然PHP、JSP等脚本均提供了MySQL的接口,但是显然直接使用C语言具有更好的安全性和性能,Michael以前用PHP开发的多个项目中就使用了C语言编写的这类接口,然后再编译到php里面,供php脚本直接使用,这方面的话题就不多说了,下面主要说一下在Linux下如何用C语言连接MySQL数据库,并且读取里面的数据返回,同时如何进行编译。 if defined(_WIN32) || defined(_WIN64)为了支持windows平台上的编译#includewindows.h#endif#includestdio.h#includestdlib.h #includemysql.h 我的机器上该文件在/usr/local/include/mysql下 定义MySQL数据库操作的宏,也可以不定义留着后面直接写进代码 defineSELECT_QUERYselectusernamefromtbb_userwhereuserid=%dintmain(intargc,char**argv)char**argv相当于char*argv[]{MYSQL mysql,*sock;定义数据库连接的句柄,它被用于几乎所有的MySQL函数 MYSQL_RES *res;查询结果集,结构类型 MYSQL_FIELD *fd ;包含字段信息的结构 MYSQL_ROW row ;存放一行查询结果的字符串数组 char qbuf[160];存放查询sql语句字符串 if(argc!=2){//检查输入参数 fprintf(stderr,usage:mysql_selectuserid\n\n);exit(1);}mysql_init(mysql);if(!(sock=mysql_real_connect (mysql,localhost,dbuser,dbpwd,9tmd_bbs_utf8,0,NULL,0))){ fprintf(stderr,Couldn'tconnecttoengine!\n%s\n\n,mysql_error(mysql));perror();exit(1);}sprintf(qbuf,SELECT_QUERY,atoi(argv[1])); if(mysql_query(sock,qbuf)){ fprintf(stderr,Queryfailed(%s)\n,mysql_error(sock));exit(1);}if(!(res=mysql_store_result(sock))){fprintf(stderr,Couldn'tgetresultfrom%s\n,mysql_error(sock));exit(1);}printf(numberoffieldsreturned:%d\n,mysql_num_fields(res));while(row=mysql_fetch_row(res)){printf(Theruserid#%d'susernameis:%s\n,atoi(argv[1]),(((row[0]==NULL)(!strlen(row[0])))?NULL:row[0])); puts(queryok!\n);}mysql_free_result(res); mysql_close(sock);exit(0);return0;为了兼容大部分的编译器加入此行} 编译的时候,使用下面的命令 gcc -o mysql_select ./mysql_select.c -I/usr/local/include/mysql -L/usr/local/lib/mysql -lmysqlclient (-lz) (-lm) 后面两个选项可选,根据您的环境情况运行的时候,执行下面的命令 ./mysql_select 1 将返回如下结果: numberoffieldsreturned:1 Theruserid#1'susernameis:Michaelqueryok!上面的代码我想大部分都能看明白,不明白的可以参考一下MySQL提供的有关C语言API部分文档源码天空 ,各个函数都有详细说明,有时间我整理一份常用的API说明出来。
C语言怎样连接mysql数据库
mysql是有c语言接口的,安装相应库后就可以链接了,一般连接mysql的函数是mysql_connect或者mysql_real_connect(大概就是这么拼的吧。。。)可以使用mysql_query执行sql语句
mysql查询语句 C语言
#ifdef WIN32
#include windows.h
#include odbcinst.h
#include sqlext.h
#else
#include mysql.h
#include unistd.h
#define SQLHANDLE static MYSQL
#endif
#include stdarg.h
#include stdlib.h
#include stdio.h
#include string.h
SQLHANDLE hDBEnv, hDBC;
int DB_Open(char * dbcn, char * usr, char * pwd)
{
int r;
#ifdef WIN32
r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, hDBEnv);
if(r) return 0;
r = SQLSetEnvAttr(hDBEnv,SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3,0);
if(r) return 0;
r = SQLAllocHandle(SQL_HANDLE_DBC, hDBEnv, hDBC);
if(r) return 0;
r = SQLConnect(hDBC,
(unsigned char *)dbcn, strlen(dbcn),
(unsigned char *)usr, strlen(usr),
(unsigned char *)pwd, strlen(pwd));
return r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO;
#else
mysql_init(hDBC);
MYSQL * rx = mysql_real_connect(
hDBC, dbcn, usr, pwd, NULL, 0, NULL, 0);
if(!rx) return 0;
return 1;
#endif
}//end DB_Open
int DB_Exec(char * sql)
{
#ifdef WIN32
SQLHANDLE hStatement = NULL;
SQLAllocHandle(SQL_HANDLE_STMT, hDBC, hStatement);
SQLExecDirect(hStatement,(unsigned char *)sql, strlen(sql));
SQLCloseCursor(hStatement);
SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
hStatement = NULL;
#else
mysql_real_query (hDBC, sql, strlen(sql));
#endif
return 1;
}//end DB_Exec
int DB_Close(void)
{
#ifdef WIN32
SQLDisconnect(hDBC);
SQLFreeHandle(SQL_HANDLE_DBC, hDBC);
SQLFreeHandle(SQL_HANDLE_ENV, hDBEnv);
#else
mysql_close(hDBC);
#endif
return 1;
}//DB_Close()
#ifndef WIN32
typedef struct tagMySQLRecordset {
MYSQL_RES * hRecord;
void * * row;
int * size;
int cols;
} MYSQLRecordset;
#endif
void DB_CleanQuery(void *hRecordset)
{
if(!hRecordset) return;
#ifdef WIN32
__try {
SQLCloseCursor(hRecordset);
SQLFreeHandle(SQL_HANDLE_STMT, hRecordset);
hRecordset = NULL;
}__finally{
return;
}//end try
#else
MYSQLRecordset * hRec = (MYSQLRecordset *) hRecordset;
mysql_free_result (hRec-hRecord);
free(hRec-row); hRec-row = NULL;
free(hRec-size); hRec-size = NULL;
free(hRec); hRec = NULL;
#endif
}//end DB_CleanQuery
int DB_Next(void * hRecordset)
{
int r=0; if(!hRecordset) return 0;
#ifdef WIN32
r = SQLFetch(hRecordset);
r = r == SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO;
if(!r) {
DB_CleanQuery(hRecordset);
}//end if
#else
MYSQLRecordset * hRec = (MYSQLRecordset *) hRecordset;
MYSQL_ROW row = mysql_fetch_row (hRec-hRecord);
if(row) {
for(int i=0; ihRec-cols; i++) {
memcpy(hRec-row[i], row[i], hRec-size[i]);
}//next i
r = 1;
}else{
DB_CleanQuery(hRecordset);
r = 0;
}//end if
#endif
return r;
}//end DB_Next
int DB_params_count(const char * fmt)
{
int i=0, j=0;
while(fmt[i]) {
if(fmt[i]=='%') j++;
i++;
}//end while
return j;
}//end DB_params_count
void * DB_Query(char *sql, const char *fmt, ...)
{
int r=0;
int cols = DB_params_count(fmt);
if(cols1) return NULL;
#ifdef WIN32
SQLHANDLE hStatement = NULL;
SQLAllocHandle(SQL_HANDLE_STMT, hDBC, hStatement);
r = SQLExecDirect(hStatement,(unsigned char *)sql, strlen(sql));
r = r==SQL_SUCCESS || r == SQL_SUCCESS_WITH_INFO;
if(!r) {
return NULL;
}//end if
#else
r = mysql_real_query (hDBC, sql, strlen(sql));
if(r) return NULL;
MYSQL_RES * rec = NULL;
rec = mysql_store_result (hDBC);
if(!rec) return NULL;
MYSQLRecordset * hStatement
= (MYSQLRecordset *)malloc(sizeof(MYSQLRecordset));
if(!hStatement) return NULL;
memset(hStatement, 0, sizeof(MYSQLRecordset));
hStatement-hRecord = rec;
hStatement-cols = cols;
hStatement-row = (void **)malloc(cols * sizeof(void *));
memset(hStatement-row, 0, cols * sizeof(void *));
hStatement-size = (int *)malloc(cols * sizeof(int));
memset(hStatement-size, 0, cols * sizeof(int));
#endif
va_list ap; va_start(ap, fmt);
void * var; char buf[32];
int i=0,j=0,k=0,sz=0; char c=0;
int len = strlen(fmt); int bad=1;
#ifdef WIN32
int col=1;
#else
int col=0;
#endif
while(fmt[i]) {
c = fmt[i++];
if(c != '%') continue;
c = fmt[i++];
var = va_arg(ap, void *);
if(c == 'd') {
#ifdef WIN32
SQLBindCol(hStatement, col, SQL_C_SLONG, var, 4,NULL);
#else
hStatement-row[col] = var;
hStatement-size[col] = sizeof(long);
#endif
col++;
continue;
}//end if
if(c == 'f') {
#ifdef WIN32
SQLBindCol(hStatement, col, SQL_C_FLOAT, var, 4,NULL);
#else
hStatement-row[col] = var;
hStatement-size[col] = sizeof(float);
#endif
col++;
continue;
}//end if
memset(buf, 0, 32); bad=1;
for(j=i-1; jlen; j++) {
c = fmt[j];
if(c='0' c='9') buf[j-i+1]=c;
if(c=='s') {bad=0; k=j+1; break;}
}//next j
if(bad) return NULL;
sscanf(buf, "%d", sz);
#ifdef WIN32
SQLBindCol(hStatement, col, SQL_C_CHAR, var, sz, NULL);
#else
hStatement-row[col] = var;
hStatement-size[col] = sz;
#endif
col++;
i = k;
}//end while
va_end(ap);
return hStatement;
}//end DB_Rec
mysql与c语言连接,代码求解释。
mysql是有c语言接口的,安装相应库后就可以链接了,一般连接mysql的函数是mysql_connect或者mysql_real_connect(大概就是这么拼的吧。。。)可以使用mysql_query执行sql语句