MysqlcAPI access database bitsCN.com
# Include
# Include "CMySQL. h"
# Include "stdio. h"
# Define MYSQL_PORT 3306
Void process_result_set (MYSQL, MYSQL_RES * result );
Void exit (MYSQL mydata, char * ep );
Int main (int argc, char * argv []) {
MYSQL mydata;
Database_Param p;
Char queryName [600];
MYSQL_RES * result = NULL;
Int status = 0;
// Initialization
Strcpy (p. host, "127.0.0.1 ");
Strcpy (p. user, "root ");
Strcpy (p. password, "root ");
Strcpy (p. db, "im ");
P. port = MYSQL_PORT;
// Initialize the data structure
If (mysql_init (& mydata) = NULL)
{
Printf ("init mysql data stauct fail/n ");
System ("pause ");
Return-1;
}
// Connect to the database
If (argc = 1)
{
If (NULL = mysql_real_connect (& mydata, p. host, p. user, p. password,
P. db, p. port, NULL, CLIENT_MULTI_STATEMENTS ))
{
Printf ("connect database fail, % s/n", mysql_error (& mydata ));
System ("pause ");
Return-1;
}
}
Else
{
Printf ("run parameter error/n ");
System ("pause ");
Return-1;
}
// First set the character set
Strcpy (queryName, "set names gbkd ");
If (mysql_query (& mydata, "set names gbk ")! = 0)
{
Exit (mydata, "set names fail ");
Return-1;
}
// Query data (single field and multiple fields are supported and output layout is supported)
Strcpy (queryName, "show databases ");
If (mysql_query (& mydata, queryName )! = 0)
{
Exit (mydata, "execute SQL syntax fail ");
Return-1;
}
// Obtain the query result
Result = mysql_store_result (& mydata );
Process_result_set (mydata, result );
// Process multiple insert statements. the last parameter in the mysql_real_connect (...) function is changed to CLIENT_MULTI_STATEMENTS.
Strcpy (queryName, "insert into fangl. admin (username, password) values ('fangl1', 'fangl1'), ('fangl2', 'fangl2'), ('fangl3', 'fangl3 ');/
Insert into fangl. admin (username, password) values ('fl1', 'fl1'), ('fl2', 'fl2'), ('fl3', 'fl3 ');/
Insert into fangl. admin (username, password) values ('fal1', 'fal1'), ('fal2', 'fal2'), ('fal3', 'fal3 ');/
Select * from fangl. admin ");
If (mysql_query (& mydata, queryName )! = 0)
{
Exit (mydata, "insert values fail ");
Return-1;
}
Do {
/* Did current statement return data? */
Result = mysql_store_result (& mydata );
If (result)
{
/* Yes; process rows and free the result set */
Process_result_set (mydata, result );
Mysql_free_result (result );
}
Else/* no result set or error */
{
If (mysql_field_count (& mydata) = 0)
{
Printf ("% lld rows affected/n ",
Mysql_affected_rows (& mydata ));
}
Else/* some error occurred */
{
Printf ("cocould not retrieve result set/n ");
Break;
}
}
/* More results? -1 = no,> 0 = error, 0 = yes (keep looping )*/
If (status = mysql_next_result (& mydata)> 0)
Printf ("cocould not execute statement/n ");
} While (status = 0 );
// Delete the data in the table
Strcpy (queryName, "delete from fangl. admin where username = 'fl1 '");
If (mysql_query (& mydata, queryName )! = 0)
{
Exit (mydata, "execute SQL syntax fail ");
Return-1;
}
// Query data (single field and multiple fields are supported and output layout is supported)
Strcpy (queryName, "select * from fangl. admin ");
If (mysql_query (& mydata, queryName )! = 0)
{
Return-1;
}
// Obtain the query result
Result = mysql_store_result (& mydata );
Process_result_set (mydata, result );
// Release result
If (result! = NULL)
{
Mysql_free_result (result );
}
Mysql_close (& mydata );
System ("pause ");
Return 1;
}
Void process_result_set (MYSQL mydata, MYSQL_RES * result)
{
Int rowcount = mysql_num_rows (result); // The number of rows in the query result.
// Obtain the field names
MYSQL_FIELD * fields = NULL;
For (int I = 0; fields = mysql_fetch_field (result); I ++)
{
Printf ("% s", fields-> name );
}
Printf ("/n ");
// Read each record in sequence
MYSQL_ROW currow = NULL;
While (currow = mysql_fetch_row (result ))! = NULL)
{
For (int I = 0; I <mysql_num_fields (result); ++ I)
{
Printf ("% s", currow [I]? Currow [I]: "NULL ");
}
Printf ("/n ");
}
}
Void exit (MYSQL mydata, char * ep)
{
Mysql_close (& mydata );
Printf ("% s, % s/n", ep, mysql_error (& mydata ));
System ("pause ");
}
The above code is compiled and verified in vs 2008. if you have any questions
This article is from the "lynn" blog
BitsCN.com