怎麼系統分類裡面沒有CGI啊?
最近好久沒有發原創文章了,都在架構服務器,編寫CGI程序
開頭用了些天perl,後來發現對腳本語言尤其是特別牛X的正則表達式有些看不懂...
回頭用C語言寫高效率的吧,反正我自己寫過好些C的庫了...
下面貼一個mysql的庫,叫做dmysql
解壓縮以後,make ; make install 安裝
然後編譯程序的時候,包含dmysql.h頭文件,加上 -ldmysql標識,即可
頭裡面定義了一個mysql數據庫的結構體,
typedef struct _dmysql_info
{
char *host; /*host for database*/
char *user; /*user name for database*/
char *pswd; /*password to the account*/
char *dbbs; /*name of database*/
} dmysql_info;
記錄集的數據類型
typedef struct _dmysql_record
{
char *s_str;
} dmysql_record;
typedef struct _dmysql_row
{
dmysql_record *s_record;
} dmysql_row;
typedef struct _d_mysql_res
{
int row;
int field;
dmysql_row *s_row;
} dmysql_res;
還有程序運行時候的出錯代碼
#define DMYSQL_QUERY_OKAY 0
#define DMYSQL_SELECT_OKAY 0
#define DMYSQL_CONNECT_ERROR -1
#define DMYSQL_INIT_ERROR -2
#define DMYSQL_QUERY_ERROR -3
#define DMYSQL_RES_ERROR -4
程序一共就3個函數
extern int dmysql_query( const char * s_query, const dmysql_info mysql_info );
給出一句執行的SQL命令,比如UPDATE、INSERT,指定一個數據庫,就可以了
extern int dmysql_select( dmysql_res **rel_res, const char *s_select, const dmysql_info mysql_info );
傳入一個數據庫記錄集的地址,給出一個SELECT記錄的SQL命令,再制定一個數據庫,就可以了
用來釋放內存中在棧裡面申明了的記錄集空間
extern void dmysql_free( dmysql_res *res );
下面是演示的程序代碼:
#include <stdio.h>
#include <dmysql.h>
int main( void )
{
dmysql_info db;
db.host="127.0.0.1"; /*host for database*/
db.user="root"; /*user name for database*/
db.pswd=""; /*password to the account*/
db.dbbs="mysql"; /*name of database*/
char *s_query="GRANT ALL ON *.* to 'dorainm'@'127.0.0.1' IDENTIFIED BY '******';";
char *s_sql="SELECT `user`,`host`,`password` FROM `user`;";
int i, j;
dmysql_res *res;
dmysql_query( s_query, db );
printf( "select out : %d\n", dmysql_select( &res, s_sql, db ) );
for( i=0; i<res->row; i++ )
{
for( j=0; j<res->field; j++ )
{
printf("%s\t", ((res->s_row+i)->s_record+j)->s_str );
}
printf( "\n" );
}
dmysql_free( res );
return 0;
};
make test後運行./dmysql
dorainm@desktop:~/workroom/c/mylib/dmysql$ make test gcc -Wall -O3 -o dmysql main.c -ldmysql `mysql_config --libs` `mysql_config --cflags` dorainm@desktop:~/workroom/c/mylib/dmysql$ ./dmysql
select out : 5
root localhost %^$#!@%*&!
root desktop.dorainm.org %^$#!@%*&!
desktop.dorainm.org %^$#!@%*&!
localhost %^$#!@%*&!
dorainm 127.0.0.1 %^$#!@%*&! dorainm@desktop:~/workroom/c/mylib/dmysql$