Mysql database encapsulated by dmysql
Mysql database encapsulated by dmysql
Why is there no cgi in the system category?
I haven't published any original articles for a long time. I 've been writing CGI programs on the architecture server.
It started with perl for a few days, and later found that some of the regular expressions in the scripting language, especially the special ox X, cannot be understood...
Use C language to write highly efficient data. I have written a lot of C libraries myself...
The following is a mysql database called dmysql.
After decompression, make; make install
Then, when the program is compiled, it contains the dmysql. h header file and the-ldmysql ID.
The header defines the structure of a mysql database,
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 the 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 is also an error code when 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 three functions.
Extern int dmysql_query (const char * s_query, const dmysql_info mysql_info );
Give an executed SQL command, such as UPDATE and INSERT, and specify a database.
Extern int dmysql_select (dmysql_res ** rel_res, const char * s_select, const dmysql_info mysql_info );
Input the address of a database record set, give an SQL command for the SELECT record, and create a database.
Used to release the record set space stated in the stack in the memory
Extern void dmysql_free (dmysql_res * res );
The Demo code is as follows:
# Include
# Include
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' @ '2017. 0.0.1 'identified '******';";
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 Row; I ++)
{
For (j = 0; j Field; j ++)
{
Printf ("% s \ t", (res-> s_row + I)-> s_record + j)-> s_str );
}
Printf ("\ n ");
}
Dmysql_free (res );
Return 0;
};
Run./dmysql after make test
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 topics top.dorainm.org % ^ $ #! @ % *&!
Topics top.dorainm.org % ^ $ #! @ % *&!
Localhost % ^ $ #! @ % *&!
Dorainm 127.0.0.1% ^ $ #! @ % *&! Dorainm @ desktop :~ /Workroom/c/mylib/dmysql $