当前位置:网站首页>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
边栏推荐
- Storage principle inside mongodb
- 一文读懂数仓中的pg_stat
- RealBasicVSR测试图片、视频
- [QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
- [untitled]
- JS slow motion animation principle teaching (super detail)
- Mongodb replication (replica set) summary
- 工具箱之 IKVM.NET 项目新进展
- Scrapy教程经典实战【新概念英语】
- About how appium closes apps (resolved)
猜你喜欢
ESP32构解工程添加组件
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
[Presto profile series] timeline use
ESP32 ① 编译环境
Introduction and basic use of stored procedures
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Cinnamon taskbar speed
靠卖概念上市,认养一头牛能走多远?
xshell连接服务器把密钥登陆改为密码登陆
cmake 学习使用笔记(一)
随机推荐
MongoDB内部的存储原理
MongoDB 分片总结
[untitled]
PACP学习笔记三:PCAP方法说明
About the problem of APP flash back after appium starts the app - (solved)
Scrapy教程经典实战【新概念英语】
php——laravel缓存cache
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
JS中为什么基础数据类型可以调用方法
高端了8年,雅迪如今怎么样?
Differences between MySQL storage engine MyISAM and InnoDB
Esp32 construction engineering add components
xshell连接服务器把密钥登陆改为密码登陆
DrawerLayout禁止侧滑显示
“新红旗杯”桌面应用创意大赛2022
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
[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
如何让join跑得更快?
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images