How to install MySQL and use its c API in Windows

Source: Internet
Author: User
Tags mysql command line
This section describes how to install MySql in windows and some basic applications. It also describes how to use its Capi. 1. install and use MySQL.
1. Install MySQL and MySQL.
2. Start MySQL, enter C:/MySQL/bin, and click winmysqladmin. When you first enter MySQL, you must enter the user name and password to generate the my. ini file in the system directory. Therefore, you must search for my. ini and delete it when uninstalling MySQL, so that it will not affect the next installation. However, you can use net start MySQL to start MySQL later and disable net stop MySQL. You can enter C:/MySQL/bin/MySQL for testing.
3. User Management: there are only two users at the time of installation. One is root, the other is empty, and their passwords are empty. Set the password for the root user first. C:/MySQL/bin/mysqladmin-u Root Password "1234567 ". Mysql-u root-P. after entering the password, you can see the MySQL prompt,
Show databases; to view all databases, remember to enter a semicolon. Press enter to start a new line.
Use MySQL; select MySql,
Show tables from MySQL; view all tables in the MySQL database. The user table records information such as user name, password, and user permission.
Select host, password, user from user; there are too many columns in the user, and the three pieces of information are similar. All the usernames and permitted logon addresses are listed in the select host, password, and user from user. Here, the anonymous user is deleted first, delete from user where user = "", and then the host is not deleted locally. Delete form user where host = www. *. com, now only one root user can log on to my database from the local machine. Add a common user as follows, assuming that the user can log on from any address and has all permissions, grant all privileges on *. * To sagely @ "%" identified by 'Password' with grant option. Again, select host, password, user from user; and you will see an extra sagely user. He can log on from any address. The same is true for adding another user, but the permission control may be different, you can insert a record into a user, for example, insert into user values ('localhost', 'sage', password ('20170101'), 'y', 'y ', 'y', 'y',);. The specific Y is determined by the permission. This allows you to manage MySQL database users.
4. insert, delete, modify, and query data.
This section mainly applies SQL statements. (In the MySQL command line)
Create Database RA; Create a database named Ra.
Use Ra; Create Table usr_info (C varchar (15), St varchar (15), L varchar (15), O varchar (15), u varchar (15 ), CN varchar (15) creates a table named usr_info, which has six attributes.
Insert into usr_info values ("cn", "DD", "ee", "ee") insert specific data. And so on. They are the same as SQL statements.
Ii. Use of C API
1. Environment Settings
Add a C:/MySQL/include to include C:/MySQL/lib in toos-> Option-> directories in VC, add a C:/MySQL/lib to Lib, and set libmysql. DLL and libmysql. copy lib to the current directory and copy libmysql. add lib to the project.
2. Establish a connection with the server.
Include <mysql. h> // contains the header file.
MySQL * conn; // defines a MySQL type structure, which is required in almost all subsequent programming.
MySQL * mysql_init (MySQL * conn); // If conn = NULL, a newly allocated handle structure is returned. Otherwise, the structure is reinitialized. If an error occurs, null is returned;
MySQL * mysql_real_connect (MySQL * connection, const char * server_host, const char * SQL _urs_name, const char * SQL _password, const char * db_name, unsigned int port_number, const char * unix_socket_name, unsigned int flags); // connect to the MySQL server. The parameters should be clear. Generally, the values of port_number and unix_socket_name are 0 and null. For example:
Mysql_real_connect (conn_ptr, "localhost", "sagely", "secret", "ra", 0, null, 0 );
Void mysql_close (MySQL * connection); close a connection.
3. Execute the SQL statement
Mysql_select_db (MySQL * connection, const char * db_name); select a database.
Int mysql_query (MySQL * connection, const char * query) // the query statement is executed. If the query statement is run successfully, 0 is returned. For example, mysql_query (conn_ptr, create table cert_info (...), A table named cert_info will be created, and almost all SQL statements can be executed in this way. Haha, isn't it easy? It's similar to the SQL statement entered in the command line. When using the above functions
My_ulonglong mysql_affected_rows (MySQL * connection). This function returns the number of rows modified by mysql_query.
4. process the Retrieved Data
After we can execute SQL statements, it is easy to execute operations such as adding, deleting, modifying, and so on. But where can we store the Retrieved Data?
The mysql_res * mysql_use_result (MySQL * connection) function returns a result set pointer to a connection object. If an error occurs, null is returned. However, it only returns a collection object initialization to receive data, and does not return any retrieved data to the result set.
Mysql_row mysql_fetch_row (mysql_res * result): This function obtains the result struct obtained by mysql_use_resul, retrieves a single row from it, and returns the data allocated to the row struct. No more data or errors occur, returns null.
Unsigned int mysql_field_count (MySQL * connection) It gets a connection object and returns the number of fields in the collection.
So we can understand the following section on the basis above: (no error handling)
Mysql_init (& conn_ptr );
Mysql_real_connect (conn_ptr, "localhost", "sagely", "secret", "ra", 0, null, 0 );
Mysql_query (conn_ptr, "select * Form usr_info );
Res_ptr = mysql_use_result (& conn_ptr );
Sqlrow = mysql_fetch_row (res_ptr );
For (INT I = 0; I <mysql_field_count (& conn_ptr); I ++) {printf ("% s", sqlrow );}
Mysql_field * mysql_fetch_field (mysql_res * result) returns information about the column, such as the column name and the Table Name of the column, with this, you can match the column title with the data.
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.