MySQL5CAPI access database example program _ MySQL

Source: Internet
Author: User
**************************************** * ********************** LQU @ 7gE0m is the result of mysql5.0, describes and uses standard c ++ to demonstrate how to use the MySQL [87PHCAPI function to operate a database in a simple way; T + E {PA ~ 3. the example program passes cKAusPI debugging on VC6 + windows2000. ******/********************************* * ****************************** LQU @ 7gE0 The following is the result of studying mysql 5.0. it is described and demonstrated using the standard c ++ to use MySQL [87PH
C api function: simple database operation process; T + E {PA ~ 3
The example program is debugged on VC6 + windows 2000 using cKAusP = I.
**************************************** * ***********************/A [s <+ P"
# Include 7a7g' my
# Include Hkp> }:#! O _
# Include // The file is located in the F iF7hj4ld Directory of the c api provided by MySQL.
Using namespace std; UuWny}>
Q5x t. kuY
// Add-lmysql qwe yX to linux and other systems)
# Pragma comment (lib, "libmysql. lib")> OY} SzU
If @ 1PPG5
07) 7f [
/*************************************** * *************************/DW, r! UI (
/// Name: main & H [l 8>
// Function: main test function $ S ~ +) '-C
// Access: private Zhf} SMRJ}
// Para: 2> 3bqd |
// 1.: int argc NWg! K!
// Number of system parameters: 'C | # e
// 2.: char * argv [] = pF Vw @ 8
//: The parameter value ICD # RY &
// Return: The exit parameter returned to the startup function <) c4] PW
// Author: hzh nR? K * E & # z
// Date: 2006-06-24 O0 "U = A {O \
/*************************************** * ************************/ZB ^ R \ 2 J
Int main (int argc, char * argv []) cr, ZH: Z
{F * Y353gB
MYSQL mydata; 7 (l: l
Fu Ez-h
// Initialize the data structure 7 + {w5 \ 4A
If (mysql_init (& mydata) = NULL) G4G ?; 0 ~ \ 4
{LpZe B 'Aj
Std: cout <"init mysql data stauct fail" < Return-1; yQ ;{? N, 5 } KZKHXM?
Nd 'HTI
// Connect to database o m ^> G "!
If (argc = 1) <-~ Fpq | 4x
{NYa5f-<
If (NULL = mysql_real_connect (& mydata, "127.0.0.1", "root", "mysql5 ",! _ CP ajA
"Hzhdb", MYSQL_PORT, NULL, 0) fS (| z37 ^
{T6 | KX "NNlO
Std: cout <"connect database fail" < Return-1; TZv [B &/
} NKkdL
} Else if (argc = 5) p-Vce # ^ no
{; 8kj1a-i V
If (NULL = mysql_real_connect (& mydata, argv [1], ++ ySQt
Argv [2], argv [3], argv [4], MYSQL_PORT, NULL, 0) e1Dl7 ^] 9 <
{WP '$ n x:
Std: cout <"connect database fail" < Return-1; t ['! + W!
} A3ied; 1) I
} EjvU * 9 H QQ
Else s; mMe m]
{V $ k1 "s,
Std: cout <"run parameter error" < Return-1; M>! * 6) a +
} L7Y 8D)
-Npl (^ K
Std: string s_ SQL = "drop table hzhtest"; DRA (+ = %
If (mysql_query (& mydata, s_ SQL .c_str ())! = 0) 0 uf 'gbjf
{Xr % 3! L?
// Failed to delete table pG {: RU Mysql_close (& mydata); O "$-@ c *
Std: cout <"drop table fail" < } N )! = % * 7
Ks {qrYZK
// Create a data table. The field myid sets the auto-incrementing column attribute a | 20-c1) c
S_ SQL = "create table hzhtest ("; p1xJ) N8Z {
S_ SQL + = "myid integer not null auto_increment,"; 8tv6 ?} T |]
S_ SQL + = "mytime datetime null, myname varchar (30),";> fh [Z: tf
S_ SQL + = "primary key (myid)"; # I = R6
If (mysql_query (& mydata, s_ SQL .c_str ())! = 0) + jP} /
{3 l _ yx}
// Failed to create table _ SrVC $ j
Mysql_close (& mydata); ^ Cd/Ox | u
Std: cout <"create table fail" < < Return-1; mh '8k [H
} K d OY 06 <
SFSs) rD
// Insert data into the table/-Qu, T)
For (int k = 1; k <30; ++ k) w7h) Ke @ n
{OU # H-Xq
S_ SQL = "insert into hzhtest (mytime, myname) values"; aShy69k: _ V
S_ SQL + = "('2017-06-"; R9 $4 woWi
Char buff [20];) NNs7uN/
Memset (buff, 0, sizeof (buff); xI "'3) q
Itoa (k, buff, 10); F: YQt _
S_ SQL + = buff; J5UJ) Gdb
@. Qj $ e {e
S_ SQL + = ""; K (Y4MVI {U;
I. uji VvK
Int I = k % 3; (8V5kf}
Memset (buff, 0, sizeof (buff);-k: {H; SK
Itoa (I, buff, 10); clErn0M
S_ SQL + = buff; Sb "{B08q1
S_ SQL + = ": 01: 01 '"; EM *) nW ^ = j
O !. BLkO)
If (I = 0) ov. \ wNe
{= 8) \ z $ B
S_ SQL + = ", NULL"; | YTM '8 \} LG
} \> M * FA
Else C. U9 (DfA/5
{QNp5qZt +
S_ SQL + = ", 'Huang Zhihui"; s! DxV
S_ SQL + = buff; IPhbq.
S_ SQL + = "'"; 6: IWmt $ S
} O @ Q ^ j + #
S_ SQL + = ")"; qx # z %: Ep
] LbE \ q ),
If (mysql_query (& mydata, s_ SQL .c_str ())! = 0) + * $ c |
{]} 3Xyw
// An error occurred while executing the SQL statement c> M * | (,
Std: cout <"execute insert syntax fail" <2gq + mqEm
Endl < Mysql_close (& mydata); kuh. c #
Return-1; \ Z c/-s \
} CeoSPr '#
} W ?? NGBL
L AU> JDX
// Query data and display 98} A pK
S_ SQL = "select myid, mytime, myname from hzhtest"; YpXA

If (mysql_query (& mydata, s_ SQL .c_str ())! = 0) e & Nm % # 9Z;
{+ N1) a7j
// An error occurred while executing the SQL statement = PaE \
Mysql_close (& mydata );/ Std: cout <"execute SQL syntax fail" <n8SbdhL
Endl <
Return-1; @ t ^ & @: C
} Y3I @} f ^ 5
2 QVwmITn
F + uY1; \ XOS
MYSQL_RES * result = mysql_store_result (& mydata); Oq # Sh
MJ8z! D-kr
// Obtain the query result I ;! @ '0
Int rowcount = mysql_num_rows (result); MbBoxy
// Obtain valid records) U # Q 1 {O
Std: cout <"exec SQL:" < ^ H! _ E99
H0? J'pv \ 0e
MYSQL_FIELD * fields = NULL; xp NRV = U/
// Obtain the field names j/ds
For (int I = 0; fields = mysql_fetch_field (result); ++ I) {'28> So
{G>-ZXEq
Std: cout < Name <"\ t"; Fc _ & UL "47
}! AcyLmP '\
Std: cout < 7m' (MVXg
, R8Ix"
// Read the dH C % 4IW "R of each record in sequence
MYSQL_ROW currrow = NULL; e! A: 'w7lm
While (currrow = mysql_fetch_row (result ))! = NULL) EG F4X4srl
{+ N? ETS
// Read row record jx9mC9] e [
For (int I = 0; I <mysql_num_fields (result); ++ I) rchlorophyll
{TpW. ne & ZR
Std: cout <(currrow ? Currrow: "NULL") <"\ t"; k e; j [#{
} Q6yF >$;
Std: cout < } 6NK [d
@ Ulr0ifw
Mysql_free_result (result); UbY 'f! 2
Mysql_close (& mydata); Q "4 ;! Mme'
Vpgtt (4 X
System ("pause"); Oy5v. Z $ J7
K/N53 $
Return 1; _ 9 }

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.