Dmysql own encapsulated Mysql library _mysql

Source: Internet
Author: User
Tags error code
How is the system classified without CGI?

I haven't made an original article in the last few days, all in the architecture server, write CGI program
The beginning took some days to Perl, and later found that the regular expressions of the scripting language, especially the special Bull X, were somewhat obscure ...
Back in C language to write efficient bar, anyway, I have written a lot of C library ...

Here is a MySQL library, called Dmysql
After decompression, make; Make install Installation
Then compile the program, including the dmysql.h header file, plus the-ldmysql logo, you can

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 recordset
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;



and the error code at the time of the program 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 3 functions in total.
extern int Dmysql_query (const char * s_query, const dmysql_info mysql_info);

Give an execution of the SQL command, such as update, INSERT, specify a database, you can


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 a select record, and then create a database, you can


To release the recordset space in memory that is declared in the stack.

extern void Dmysql_free (Dmysql_res *res);



The following is a demo program code:
#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 ' Hu Jintao ';";
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$

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.