当前位置:网站首页>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
边栏推荐
猜你喜欢
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Introduce six open source protocols in detail (instructions for programmers)
ESP32构解工程添加组件
Esp32 ① compilation environment
飞桨EasyDL实操范例:工业零件划痕自动识别
【学习笔记】AGC010
xshell连接服务器把密钥登陆改为密码登陆
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
DHCP 动态主机设置协议 分析
OSI seven layer model
随机推荐
Getting started with cinnamon applet
MongoDB复制(副本集)总结
JS function 返回多个值
Test next summary
【学习笔记】线段树选做
PAcP learning note 1: programming with pcap
MATLAB中polarscatter函数使用
JNA learning notes 1: Concepts
Realbasicvsr test pictures and videos
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
LeetCode_ Binary search_ Medium_ 153. Find the minimum value in the rotation sort array
OSI 七层模型
JS缓动动画原理教学(超细节)
Practical case: using MYCAT to realize read-write separation of MySQL
《开源圆桌派》第十一期“冰与火之歌”——如何平衡开源与安全间的天然矛盾?
Esp32 construction engineering add components
MongoDB的用户管理总结
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
MongoDB优化的几点原则
RealBasicVSR测试图片、视频