Environment Settings:
After installing MySQL, install libmysql in the include directory. copy the lib file to VC \ lib \ In the VS2008 installation directory, add "c: \ MySQL \ include \" to the include directory in project-Option-c/c ++-general and the additional library directory in the linker-general \", add "libmysql. lib ", so that the compiler can find mysql. h header file, and you can use the mysql API of C language in the program to operate the database. (If the MySQL installation directory does not contain the include directory, you can download and install MySQL connector for C from the MySQL official website, and modify the include directory path)
# Include <Windows. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <mysql. h>
# Include <iostream>
Using namespace std;
Int main ()
{
Const char user [] = "root"; // username
Const char pswd [] = "root"; // password
Const char host [] = "localhost"; // or "127.0.0.1"
Const char table [] = "test"; // database
Unsigned int port = 3306; // server port
MYSQL myCont;
MYSQL_RES * result;
MYSQL_ROW SQL _row;
MYSQL_FIELD * fd;
Char column [32] [32];
Int res;
Mysql_init (& myCont );
If (mysql_real_connect (& myCont, host, user, pswd, table, port, NULL, 0 ))
{
Cout <"connect succeed! "<Endl;
Mysql_query (& myCont, "set names gbk"); // sets the encoding format. Otherwise, Chinese characters cannot be displayed in cmd.
Res = mysql_query (& myCont, "select * from samples"); // query
If (! Res)
{
Result = mysql_store_result (& myCont); // Save the queried data to the result
If (result)
{
Int I, j;
Cout <"number of result:" <(unsigned long) mysql_num_rows (result) <endl;
For (I = 0; fd = mysql_fetch_field (result); I ++) // obtain the column name
{
Strcpy (column [I], fd-> name );
}
J = mysql_num_fields (result );
For (I = 0; I <j; I ++)
{
Printf ("% s \ t", column [I]);
}
Printf ("\ n ");
While (SQL _row = mysql_fetch_row (result) // obtain specific data
{
For (I = 0; I <j; I ++)
{
Printf ("% s \ n", SQL _row [I]);
}
Printf ("\ n ");
}
}
}
Else
{
Cout <"query SQL failed! "<Endl;
}
}
Else
{
Cout <"connect failed! "<Endl;
}
If (result! = NULL) mysql_free_result (result); // release the result resource
Mysql_close (& myCont); // disconnect
Return 0;
}
Code 2:
Test environment: MySQL 5.1.35
After installing MySQL, open the MySQL Command Line Client and enter the root password to operate the database.
// View the MySQL version
Mysql> select version ();
// Display all databases
Mysql> show databases;
// Use the database
Mysql> use database_name;
// Display all data tables
Mysql> show tables;
// Display the data table structure
Mysql> describe table_name;
// Create a database
Mysql> create database database_name;
// Delete the database
Mysql> drop database database_name;
// Create a data table
Mysql> use database_name;
Mysql> create table table_name (field name VARCHAR (20), field name CHAR (1 ));
// Delete a data table
Mysql> drop table table_name;
// Query records
Mysql> select * from table_name;
// Import the. SQL File
Mysql> use database_name;
Mysql> source c:/mysql. SQL
// Modify the root password
Mysql> UPDATE mysql. user SET password = PASSWORD ('new password') WHERE User = 'root ';
// Exit
Mysql> quit