dmysql's own encapsulated mysql library
- 2020-05-06 11:46:36
- OfStack
How come there is no CGI in the system classification?
Recently for a long time no original article, are in the architecture server, write CGI program
After a few days of perl, I found that I could not understand regular expressions in scripting languages, especially X.
Go back to the C language and write efficient ones. Anyway, I've written many C libraries myself...
Below is a library of mysql called dmysql
After unzipping, make ; make install install
Then, when compiling the program, include the dmysql.h header file with the -ldmysql identity to
The header defines an mysql database structure,
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;
The data type of the recordset is
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 is also the error code
while the program is running #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
The program has a total of three functions
extern int dmysql_query( const char * s_query, const dmysql_info mysql_info );
Give an SQL command to execute, such as UPDATE, INSERT, specify a database, and
will do
extern int dmysql_select( dmysql_res **rel_res, const char *s_select, const dmysql_info mysql_info );
Pass in the address of a database recordset, give an SQL command for SELECT records, and make a database,
To free up the recordset space
declared in memory on the stack
extern void dmysql_free( dmysql_res *res );
Here is the code for the demo:
#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;
};
Run after 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$
Recently for a long time no original article, are in the architecture server, write CGI program
After a few days of perl, I found that I could not understand regular expressions in scripting languages, especially X.
Go back to the C language and write efficient ones. Anyway, I've written many C libraries myself...
Below is a library of mysql called dmysql
After unzipping, make ; make install install
Then, when compiling the program, include the dmysql.h header file with the -ldmysql identity to
The header defines an mysql database structure,
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;
The data type of the recordset is
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 is also the error code
while the program is running #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
The program has a total of three functions
extern int dmysql_query( const char * s_query, const dmysql_info mysql_info );
Give an SQL command to execute, such as UPDATE, INSERT, specify a database, and
will do
extern int dmysql_select( dmysql_res **rel_res, const char *s_select, const dmysql_info mysql_info );
Pass in the address of a database recordset, give an SQL command for SELECT records, and make a database,
To free up the recordset space
declared in memory on the stack
extern void dmysql_free( dmysql_res *res );
Here is the code for the demo:
#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;
};
Run after 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$