MySQL C Programming on Windows

Source: Internet
Author: User

I. trembling

Since I started learning programming, the first real programming language I came into contact with was C (FOXBASE is not counted). I wanted to learn about the C language programming by myself, the teacher started to teach Pascal. Since graduation, I have never had the opportunity to write C Programs with application values. I have achieved J2EE from Delphi. I plan to use my spare time to learn Pro * C, MySQL from now on
C. I don't know why I never forget C, but I didn't use C to write programs.

2. Development Environment

1. WindowsXP

2. MySQL 5.0.22

3. Dev-C ++ 4.9.9.2

4. MySQL C for Dev-CPP (MySQL. devpak)

A) people who have used Dev-C ++ should know about devpak. I will not elaborate on it. If you are using C ++, please download (mysqlpp-2.3.2-gcc3.4.2-1due.DevPak)

B) mysql. devpak: http://downloads.sourceforge.net/dev-cpp/MySQL.DevPak

Iii. installation and configuration

1. Double-click the mysql. devpak file and follow the steps to install the SDK. It is enough to wait until "MySQL" appears in package mangaer.

2. Click "Tools"-> "Compilation options"-> "compiler"-> "Add the following command in the connector command line" (select ), add-lmysql in the text box below.

3. Add the MySQL lib and include files in Dev-C ++. The specific method is as follows:

"Tools"-> "Compilation options"-> "compiler"-> "directory" to add

Iv. Routine code

# Include <stdlib. h>

# Include <Winsock. h>

# Include <mysql. h>

# Include <stdio. h>

/*

Name: test example

Copyright: http://www.blogjava.net/pdw2009

Author: min Dewan

Date: 09-03-08

Description:

*/

Int main ()

{

MySQL; // MySQL connection

Mysql_res * res; // This structure indicates a query result set of the returned row.

Mysql_row row; // type-safe representation of the data in a row

Char * query; // query statement

Int T, R;

Mysql_init (& MySQL );

If (! Mysql_real_connect (& MySQL, "localhost", "lottobar", "123456", "lottobar", 3306, null, 0 ))

{

Printf ("error connecting to database: % s" N ", mysql_error (& MySQL ));

} Else

Printf ("connected..." N ");

Query = "set Character Set GBK"; // sets the encoding.

T = mysql_real_query (& MySQL, query, (unsigned INT) strlen (query ));

If (t)

{

Printf ("encoding setting failed" N ");

}

Query = "select * from demo ";

T = mysql_real_query (& MySQL, query, (unsigned INT) strlen (query ));

If (t)

{

Printf ("exception occurred during query execution: % s", mysql_error (& MySQL ));

} Else

Printf ("[% s] build successful" N ", query );

Res = mysql_store_result (& MySQL );

While (ROW = mysql_fetch_row (RES ))

{

For (t = 0; t <mysql_num_fields (RES); t ++)

{

Printf ("% s" T ", row [T]);

}

Printf ("N ");

}

Mysql_free_result (RES );

Sleep (1 );

Return 0;

}

5. Notes

If the Windows C program is configured with a network, these two files should be added; otherwise, an error will occur.

# Include <stdlib. h>

# Include <Winsock. h>

How to solve MySQL C garbled characters

Query = "set Character Set GBK"; // sets the encoding.

T = mysql_real_query (& MySQL, query, (unsigned INT) strlen (query ));

If you have any questions, go to www.blogjava.net/pdw2009.



The author of the original, reproduced Please note: http://www.blogjava.net/pdw2009

Http://hi.baidu.com/leeyou1450/blog/item/78fdd438ec9986f63a87ceaa.html

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.