当前位置:网站首页>Getting started with MySQL

Getting started with MySQL

2022-07-07 13:29:00 MirrorYuChen

1.MySQL install

Specific installation and configuration MySQL Tutorials can refer to resources [1].

2. Database query and creation

2.1 Database query

Through the following query instructions , You can query the created database

>> show databases;

 Database query results

2.2 Database creation

Database creation can be achieved by using the following command line :

>> create database [ Database name ]

Use the query instruction again , You can see the database that has been created .
 Database creation

2.3 Database delete

Database deletion can be achieved by using the following command line :

>>  drop database [ Database name ]

Use the query instruction again , You can see that the database has been deleted successfully .
 Database delete

2.4 Database switching

When we create tables , You need to specify the database first , Then you can create , The database can be specified by the following instructions :

>> use [ Database name ];

 Specify database

3. Table creation 、 Delete 、 Insert and query

3.1 Table creation

The basic syntax for table creation is :

create table [ Table name ] (
	column1 datatype,
	column2 datatype,
	...
	columnN datatype,
	primary key(one or more colums)
);

for instance , Create a student surface :

create table student (
	id int not null,
	name varchar(20) not null,
	gender varchar(20) not null,
	age int not null,
	primary key(`id`)
);

The latter part is the constraint on the current field , Such as not null Indicates that the current field cannot be NULL,primary key Used to set the primary key of the table .
You can use the following instructions to query the structure of the currently created table :

>> desc [ Table name ]

 Create and query tables
If you want to rename the table , You can use the following instructions :

>> rename [ The old name of the table ] to [ The new name of the table ];

3.2 Table delete

Table deletion is simple , You can use the following instructions

>> drop table [ Table name ];

3.3 Table insert

Table insertion uses the following instructions :

>> insert into [ Table name ] values ([ Table value ]);

for example , The table created earlier student, Insert two rows of data into it :

>> insert into student values (1, ' Zhang San ', ' male ', 20);
>> insert into student values (2, ' Li Si ', ' male ', 21);

3.4 Table query

Table queries can use the following instructions :

>> select * from [ Table name ];

 Table query

3.5 Table delete

The following instructions can be used to delete the data in the table :

>> delete from [ Table name ] where [ Delete the condition ]

Here is for the follow-up experiment , Insert a row of data first : Wang two pock marks , Then delete this line of data .
 Table delete

4. C++ Code to query the database

4.1 MySQL Library Configuration

If you are familiar with OpenCV The configuration process , This is very simple , There are three main steps :
(1) Library dll Path to the system environment variable , Restart the computer after adding , System variables take effect :
dll Add the library to the system environment variable
(2) Create a visual studio engineering , Notice to switch to x64 Environmental Science , Add main.cpp, To configure MySQL Of include Location :
 To configure MySQL Of include
(3) To configure MySQL Of lib Location :
 To configure MySQL Of lib
(4) Configure the library that the system needs to link :
 To configure MySQL Libraries that need to be linked

4.2 The test case

#include <mysql.h>
#include <stdio.h>

int main() {
	const char* host = "127.0.0.1";
	const char* user = "root";           //  Change this to your user name 
	const char* passw = "123456";  //  Change this to your own password 
	const char* db = "school";         //  Change this to access the database 
	MYSQL mysql;

	// 1. Initialize database 
	mysql_init(&mysql);

	// 2. Set character encoding 
	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");

	// 3. Connect to database 
	if (mysql_real_connect(&mysql, host, user, passw, db, 3306, NULL, 0) == NULL) {
		printf(" The reason for the error :%s\n", mysql_error(&mysql));
		printf(" The connection fails !\n");
		exit(-1);
	}

	// 4. Query results 
	int ret = mysql_query(&mysql, "select * from student;");   //  This should be changed to the table to be accessed 
	printf("ret: %d.\n", ret);

	// 5. To get the results 
	MYSQL_RES* res = mysql_store_result(&mysql);

	// 6. Print the query results 
	MYSQL_ROW row;
	while (row = mysql_fetch_row(res)) {
		printf("%s ", row[0]); // ID
		printf("%s ", row[1]); // Name
		printf("%s ", row[2]); // gender
		printf("%s \n", row[3]); // age
	}

	// 7. Release result set 
	mysql_free_result(res);

	// 8. Close the database 
	mysql_close(&mysql);

	system("pause");
	return 0;
}

The operation results are as follows , You can see the same result as using the command line query :
 Running results
Enjoy!

Reference material

原网站

版权声明
本文为[MirrorYuChen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071105243898.html