Connect the C language to the MySQL database (course design summary)

Source: Internet
Author: User

The end of the course design also indicates that the holiday is coming soon. This course is not difficult to design. It is nothing more than making a XXX system to implement user registration, login, menu management, super user, and other functions. So far, I have done several times, so you are familiar with the basic process! I think the most important thing to sum up is the connection between C language and database! Because it was previously implemented using files.

★Platform

This course design mainly uses:

● VC ++ 6.0

● Mysql-5.0.18-win32

● Navicat for MySQL (MySQL graphical tool)

★Configuration

Before writing code, you must first let the compiler know some MySQL database APIs. Therefore, you need to configure database paths on the VC. I found a good reference from the Internet.

★Database API function ● mysql_init ()

Prototype: MYSQL * mysql_init (MYSQL * mysql)

Role: used to initialize MYSQL objects

● Mysql_real_connect ()

Prototype: 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 long client_flag)


Purpose: connect to a MySQL database server. The structure address of MySQL is & mysql, the host name or address is "localhost", the username is "root", and the password is "root ", the database is "test" and the port is "3306 ".

● Mysql_store_result ()

Prototype: MYSQL_RES * mysql_store_result (MYSQL * mysql)

Purpose: retrieve data and obtain the result set in the database.

● Mysql_num_rows ()

Prototype: mysql_num_rows (MYSQL_RES * result)

Purpose: obtain the number of records in the result set. Generally, mysql_num_rows () is used after mysql_store_result (). The mysql_num_rows () parameter is the return value of mysql_store_result.


● Mysql_fetch_row ()

Prototype: MYSQL_ROW mysql_fetch_row (MYSQL_RES * result)

Purpose: obtain a record in the result set. Each execution will take a row of records. Similarly, its parameters are the same as those of mysql_num_rows ().

★C array passed to SQL statement

I encountered a problem when writing to the registration function and studied it for a long time. The problem is: in C language, the user information (including the ID number, name, and password) I entered is saved in the array, in the database functions, there is only such a form:

Mytable is the name of the table in the database. It contains the fields in the table and the data to be inserted after values, but the user registration information is saved in the character array, how can I convert the data in the character array into data that complies with SQL statements?

After finding a lot of information, I finally found a function:Sprintf ()

● Its usage:

First, we need to apply for a character array, which is assumed to be SQL _insert [50]. Its function is to write data to this array in the future, that is, to write user registration information to this array first.

Then:

In this way, the user registration information can be inserted into the database. SQL _insert is the preceding array, followed by an SQL statement.

Example of Information Query:





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.