C Connect C + + windows good

Source: Internet
Author: User
Tags odbc odbc connection sprintf
Create a new Win32 Console application project, add "C:/mysql/include" to the Include path to the compilation option (Add/I "d:/mysql/include" in Project options) 。 Create a new MAIN.C file with the main body as follows:

#include
#include
#include
#include
int main (int argc, char * argv[])
{
Char sztargetdsn[] = "Test";
Char szsqltext[500]= "";
Char aszflds[25 [25];
MYSQL * MYDATA;
Mysql_res * RES;
Mysql_field * FD;
Mysql_row ROW;
int i,j,k;
BOOL bcreate = TRUE;
if ((MyData = Mysql_init ((mysql*) 0))
Initializing data structures
&& Mysql_real_connect (MyData, NULL,
Connecting to a database
"Root", "Your_password", Sztargetdsn,
Mysql_port, NULL, 0))
{
if (bcreate)
{
sprintf (Szsqltext,//construct SQL statements
"CREATE TABLE MyTable"
Create a new table
"(Time datetime, S1 char (6),"
"S2 char (one), S3 int, S4 int)");
if (mysql_query (MyData, Szsqltext))
Execute SQL statement
{//Error executing SQL statement
ErrLog ("Can ' t create TABLE");
Mysql_close (MyData);
return FALSE;
}

sprintf (Szsqltext,
"INSERT INTO MyTable"
Inserting data into a table
"Values (' 2000-3-10 21:01:30 ',"
Note The format of the time
"' Test ', ' Mysqltest ', 2000, 3");
if (mysql_query (MyData, Szsqltext))
{//Error executing SQL statement
ErrLog ("Can ' t insert data to table");
Mysql_close (MyData);
return FALSE;
}
sprintf (Szsqltext, "select * from MyTable");
if (mysql_query (MyData, Szsqltext))
For data retrieval
{
Error executing SQL statement
Mysql_close (MyData);
return FALSE;
}
Else
{
res = Mysql_store_result (myData);
Get query Results
i = (int) mysql_num_rows (res);
Number of valid records obtained
printf ("Query:%s/n%ld Records found:
/n ", Szsqltext, I);
for (i = 0; FD = Mysql_fetch_field (res);
i++)
strcpy (aszflds[i], fd->name);
Get each field name
for (I=1;   row = Mysql_fetch_row (res); )
Read each record sequentially
{j = mysql_num_fields (res);
Get the number of fields in a record
printf ("Record #%ld:-/n", i++);
for (k = 0;   K < J; k++)
Output values for each field
printf ("Fld #%d (%s):%s/n", K + 1, aszflds[K],
((row[k]==null) | |
(!strlen (row[k]))? " NULL ": Row[k]));
Puts ("==============================/n");
}
Mysql_free_result (RES);
}
}
Else
{//Connection Database error
ErrLog ("Can" T connect to the MySQL server);
Mysql_close (MyData);
return FALSE;
}
Mysql_close (MyData);
return TRUE;
}


For a brief description of several of these functions, for details, refer to the MySQL documentation:

1. mysql *mysql_init (MySQL *mysql)

Initializes a data structure of type MySQL to prepare for the execution of Mysql_real_connect (). Parameter mysql is a pointer to the structure, and if MySQL is null, new and initialize a MySQL data structure. The new structure will be freed in Mysql_close ().
If successful, returns a pointer to the initialized MySQL data structure, otherwise null is returned.

2. mysql *mysql_real_connect (MySQL *mysql, const char *host,

const char *user, const char *PASSWD, const char *DB,
unsigned int port, const char *unix_socket, unsigned int client_flag)
Establish a connection to the MySQL database engine. Before performing further data operations, it is important to ensure that the Mysql_re
Al_connect () successfully returned.
Parameter MySQL is the return value of Mysql_init ();
Parameter host is the TCP/IP hostname of the machine running the MySQL database engine, or null defaults to "Lo
Calhost ";
Parameter user and passwd are the legitimate users and passwords of the MySQL database;
Parameter db is the database name of the connection;
Parameters Port,unix_socket and Client_flag generally take the default values.

3. int mysql_query (MySQL *mysql, const char *query)

To execute the SQL statement in the query string, query must end with 0. If successful, return 0.

4. Mysql_res *mysql_store_result (MYSQL *mysql)

Returns the results of statements executed by Select,show,describe, explain, and so on. function to create a new MYSQL_ RES data structure that stores the results in that structure.   If the query does not have a matching result, an empty dataset is returned. After the result set is processed, mysql_free_result () must be invoked.

Returns NULL if an error occurs, or returns a pointer to the mysql_res structure.

5. Mysql_row mysql_fetch_row (mysql_res *result)

Returns the next record in the result set, or null if there are no records or errors. The number of fields in one record can be obtained with mysql_num_fields (result), and the values of each field can be accessed using an array of row[0] to row[mysql_nu m_fields (Result)-1].

In the project link option, the C:/mysql/lib/libmysql.lib interface library is added, the LIBMYSQL.D is copied to the operating system directory (C:/WINNT/SYSTEM32) and can be compiled and run.

Here, a simple database application is developed. Of course, MySQL has a rich set of API, you can view the document. In addition, if cross-platform portability is required, consider using MySQL's ODBC interface. You can configure MySQL's ODBC connection properties yourself, or you can download the Myodbc-2.50.29-nt.zip toolkit to configure it.


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.