SoFunction
Updated on 2025-03-08

MySQL's own encapsulated mysql library

Why is there no CGI in the system classification?

I haven't posted original articles recently, and I've been building servers and writing CGI programs.
I used perl for a few days at the beginning, and later I found that I couldn't understand scripting languages, especially the regular expressions of particularly awesome X...
Let's write efficiently in C language. Anyway, I have written many C libraries myself...

Below is a mysql library called dmysql
After decompression, make ; make install
Then when compiling the program, include the header file and add the -ldmysql tag to

The mysql database structure is defined in the header.

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;



Data type of record set
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;



There are also error codes when the program runs
#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




There are 3 functions in the program
extern int dmysql_query( const char * s_query, const dmysql_info mysql_info );

Give a sentence of executing SQL commands, such as UPDATE, INSERT, specify a database, and it's fine.


extern int dmysql_select( dmysql_res **rel_res, const char *s_select, const dmysql_info mysql_info );


Pass in the address of a database record set, give a SQL command for SELECT records, and then create a database.


Used to free up the record set space declared in the stack in memory

extern void dmysql_free( dmysql_res *res );



Here is the demo program code:
#include <>
#include <>

int main( void )
{
    dmysql_info db;
    ="127.0.0.1"; /*host for database*/
    ="root"; /*user name for database*/
    =""; /*password to the account*/
    ="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;
};

 


Run after make test./mysql

dorainm@desktop:~/workroom/c/mylib/dmysql$ make test gcc -Wall -O3 -o dmysql  -ldmysql `mysql_config --libs` `mysql_config --cflags` dorainm@desktop:~/workroom/c/mylib/dmysql$ ./dmysql

select out : 5
root localhost %^$#!@%*&!
root  %^$#!@%*&! 
  %^$#!@%*&! 
 localhost %^$#!@%*&!
dorainm 127.0.0.1 %^$#!@%*&! dorainm@desktop:~/workroom/c/mylib/dmysql$