当前位置:网站首页>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;
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 .
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 .
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 ];
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 ]
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 ];
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 .
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 :
(2) Create a visual studio engineering , Notice to switch to x64 Environmental Science , Add main.cpp, To configure MySQL Of include Location :
(3) To configure MySQL Of lib Location :
(4) Configure the library that the system needs to link :
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 :
Enjoy!
Reference material
边栏推荐
猜你喜欢
Final review notes of single chip microcomputer principle
QQ的药,腾讯的票
Fast development board pinctrl and GPIO subsystem experiment for itop-imx6ull - modify the device tree file
[untitled]
Digital IC Design SPI
Vscade editor esp32 header file wavy line does not jump completely solved
[learning notes] zkw segment tree
数字ic设计——SPI
详细介绍六种开源协议(程序员须知)
JS缓动动画原理教学(超细节)
随机推荐
存储过程的介绍与基本使用
【学习笔记】线段树选做
DETR介绍
RealBasicVSR测试图片、视频
Some principles of mongodb optimization
About how appium closes apps (resolved)
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Differences between MySQL storage engine MyISAM and InnoDB
服务器到服务器 (S2S) 事件 (Adjust)
如何让join跑得更快?
“新红旗杯”桌面应用创意大赛2022
The difference between cache and buffer
Esp32 construction engineering add components
分布式事务解决方案
Ogre入门尝鲜
JS determines whether an object is empty
User management summary of mongodb
Cookie and session comparison
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
[learning notes] agc010