MySQL c api statment and bind execution Query

Source: Internet
Author: User

# Include <string>
# Include <MySQL/MySQL. h>
# Include <default. h>

Typedef STD: String string;

Struct mysql_parm {
String host;
String user;
String password;
String database;
String unixsock;
};
Class dbstmt;
Class dbmysql;

Class dbstmt {
Dbstmt (const dbstmt &);
Dbstmt & operator = (const dbstmt &);
Mysql_stmt * stmt _;
Public:
Dbstmt (pcsz_t query, dbmysql & MySQL );
Void execute (){
If (mysql_stmt_execute (stmt _))
Throw mysql_stmt_error (stmt _);
}
 
Void execute (mysql_bind * bind ){
If (mysql_stmt_execute (stmt _))
Throw mysql_stmt_error (stmt _);
If (mysql_stmt_bind_result (stmt _, bind )){
Throw mysql_stmt_error (stmt _);
}
If (mysql_stmt_store_result (stmt _))
Throw mysql_stmt_error (stmt _);
}

// Void execute (){
// If (mysql_stmt_execute (stmt _))
// Throw mysql_stmt_error (stmt _);
//}
 
Void BIND (mysql_bind * bind ){
If (mysql_stmt_bind_param (stmt _, bind ))
Throw mysql_stmt_error (stmt _);
}
 
Int fetch (){
Return mysql_stmt_fetch (stmt _) = 0;
}
~ Dbstmt (){
If (stmt _){
Mysql_stmt_close (stmt _);
}
}
 
};

Class dbmysql {
Dbmysql (const dbmysql &);
Dbmysql & operator = (const dbmysql &);
MySQL * mysqlptr _;

Uint32_t errno _;
Protected:
Friend class dbstmt;
Mysql_stmt * _ createstmt (){
Mysql_stmt * ret = mysql_stmt_init (mysqlptr _);
If (RET)
Return ret;
Errno _ = mysql_errno (mysqlptr _);
Throw mysql_error (mysqlptr _);
}
Public:
Const char * strerr (){
Return mysql_error (mysqlptr _);
}
Dbmysql (): mysqlptr _ (null ){
Mysqlptr _ = mysql_init (null );
If (null = mysqlptr _)
Throw "MYSQL: outof memory ";
}
Void open (const mysql_parm & parm ){
If (! Mysql_real_connect (mysqlptr _,
Parm. Host. c_str (),
Parm. User. c_str (),
Parm. Password. c_str (),
Parm. database. c_str (),
0,
Parm. unixsock. c_str (),
0 ))
{
Errno _ = mysql_errno (mysqlptr _);
Throw (mysql_error (mysqlptr _));
}
}

Void close (){
If (mysqlptr _)
{
Mysql_close (mysqlptr _);
Mysqlptr _ = NULL;
}
}

};

Dbstmt: dbstmt (pcsz_t query, dbmysql & MySQL): stmt _ (null ){
Stmt _ = mysql. _ createstmt ();
If (! Stmt _)
Throw mysql. strerr ();
If (mysql_stmt_prepare (stmt _, query, strlen (query )))
{
// Const char * err =

Throw mysql_stmt_error (stmt _);
}
 
}

Struct account {
Char user [36];
Byte password [16];
Uint32_t status;
Uint32_t ID;
};

# Define decl_bind (H, N )/
Class bind _ # H: Public H {/
Typedef H parent ;/
Mysql_bind _ bind [N];/
My_bool _ is_null [N];/
Unsigned long _ length [N];/
Public :/
Bind _ ## H (){/
Int I = 0 ;/
Bzero (_ bind, sizeof (_ bind ));

# Define bind_bin (x, L )/
_ Bind [I]. buffer_type = mysql_type_string ;/
_ Bind [I]. Buffer = (char *) & (parent: X );/
_ Bind [I]. buffer_length = L ;/
_ Bind [I]. is_null = _ is_null + I ;/
_ Bind [I]. Length = _ Length + I ;/
++ I;

# Define bind_int (X )/
_ Bind [I]. buffer_type = mysql_type_long ;/
_ Bind [I]. Buffer = (char *) & (parent: X );/
_ Bind [I]. buffer_length = 0 ;/
_ Bind [I]. is_null = _ is_null + I ;/
_ Bind [I]. Length = _ Length + I ;/
++ I;

# Define end_bind (h )}/
Operator mysql_bind *(){/
Return _ bind ;/
}/
};

// Account ACC;
//
// Decl_bind (4)
// Bind_bin (Acc. User, 32 );
// Bind_bin (Acc. Password, 16 );
// Bind_int (Acc. status );
// Bind_int (Acc. ID );

// Smt.exe cuteandstore (BIND );
// End_bind (4)

Decl_bind (account, 4)
Bind_bin (user, 32)
Bind_bin (password, 16)
Bind_int (Status)
Bind_int (ID)
End_bind (account)

Int main (){

Try {

Dbmysql MySQL;
Mysql_parm parm;
Parm. Host = "localhost ";
Parm. User = "root ";
Parm. Password = "mypwd ";
Parm. unixsock = "/var/lib/MySQL. Sock ";
Parm. Database = "testdb ";
MySQL. Open (parm );

Dbstmt SMT ("Select User, password, status, ID from account", MySQL );
Dbstmt smt1 ("insert into account (user, password, status) value (?,?,?) ", MySQL );
//

Bind_account ACC;

Smt.exe cute (ACC );

While (SMT. Fetch ()){
// Acc. User [length [0] = 0;
// Acc. user,
Printf ("% S % d/N", ACC. User, ACC. Status, ACC. ID );
// Printf ("% d/N", ACC. Status, ACC. ID );

;

};
Smt1.bind (ACC );
Smt1.execute ();



} Catch (const char * ERR ){
Printf ("error: % s/n", err );
}
 
Return 0;
 
}

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.