当前位置:网站首页>MySQL入门尝鲜
MySQL入门尝鲜
2022-07-07 11:05:00 【MirrorYuChen】
1.MySQL安装
具体安装和配置MySQL教程可以参考资料[1]。
2.数据库查询和创建
2.1 数据库查询
通过下面的查询指令,就可以查询到已经创建的数据库
>> show databases;
2.2 数据库创建
数据库创建可以使用下面的命令行来实现:
>> create database [数据库名]
再次使用查询指令,可以看到已经创建的数据库。
2.3 数据库删除
数据库删除可以使用下面的命令行来实现:
>> drop database [数据库名]
再次使用查询指令,可以看到已经删除数据库成功。
2.4 数据库切换
当我们创建表的时候,需要先指定数据库,然后才能进行创建,可以通过下面指令来指定数据库:
>> use [数据库名];
3.表创建、删除、插入和查询
3.1 表创建
表创建的基本语法为:
create table [表名] (
column1 datatype,
column2 datatype,
...
columnN datatype,
primary key(one or more colums)
);
举个例子,创建一个student表:
create table student (
id int not null,
name varchar(20) not null,
gender varchar(20) not null,
age int not null,
primary key(`id`)
);
后面部分是对当前字段的约束,如not null表示当前字段不能为NULL,primary key用于设置表的主键。
可以用下面指令来查询当前创建表的结构:
>> desc [表名]
如果要对表进行重命名,可以使用如下指令:
>> rename [旧表名] to [新表名];
3.2 表删除
表删除很简单,可以使用如下指令
>> drop table [表名];
3.3 表插入
表插入使用如下指令:
>> insert into [表名] values ([表值]);
例如,前面创建的表student,向里面插入两行数据:
>> insert into student values (1, '张三', '男', 20);
>> insert into student values (2, '李四', '男', 21);
3.4 表查询
表查询可以使用如下指令:
>> select * from [表名];
3.5 表删除
删除表中数据可以使用如下指令:
>> delete from [表名] where [删除条件]
这里为了后续实验,先插入了一行数据:王二麻子,然后删除这行数据。
4. C++代码来查询数据库
4.1 MySQL库配置
如果熟悉OpenCV配置过程,这个就很简单,主要分三个步骤:
(1) 将库dll路径添加到系统环境变量,添加完毕后要重启一下电脑,系统变量才会生效:
(2) 创建一个visual studio工程,注意要切换到x64环境,添加个main.cpp,配置MySQL的include位置:
(3) 配置MySQL的lib位置:
(4) 配置系统需要链接的库:
4.2 测试用例
#include <mysql.h>
#include <stdio.h>
int main() {
const char* host = "127.0.0.1";
const char* user = "root"; // 这个改成自己的用户名
const char* passw = "123456"; // 这个改成自己的密码
const char* db = "school"; // 这个改成自己要访问数据库
MYSQL mysql;
// 1.初始化数据库
mysql_init(&mysql);
// 2.设置字符编码
mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "gbk");
// 3.连接数据库
if (mysql_real_connect(&mysql, host, user, passw, db, 3306, NULL, 0) == NULL) {
printf("错误原因:%s\n", mysql_error(&mysql));
printf("连接失败!\n");
exit(-1);
}
// 4.查询结果
int ret = mysql_query(&mysql, "select * from student;"); // 这个要改成要访问的表
printf("ret: %d.\n", ret);
// 5.获取结果
MYSQL_RES* res = mysql_store_result(&mysql);
// 6.打印查询到的结果
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.释放结果集
mysql_free_result(res);
// 8.关闭数据库
mysql_close(&mysql);
system("pause");
return 0;
}
运行结果如下,可以看到跟使用命令行查询得到相同结果:
Enjoy!
参考资料
- [1] C/C++服务器开发
- [2] SQL CREATE TABLE:创建表
边栏推荐
- What kind of methods or functions can you view the laravel version of a project?
- Guangzhou held work safety conference
- ip2long之后有什么好处?
- 2022 practice questions and mock examination of the third batch of Guangdong Provincial Safety Officer a certificate (main person in charge)
- SSM框架搭建的步骤
- RecyclerView的数据刷新
- Awk of three swordsmen in text processing
- Analysis of DHCP dynamic host setting protocol
- [learn microservice from 0] [01] what is microservice
- Conversion from non partitioned table to partitioned table and precautions
猜你喜欢
智云健康上市:市值150亿港元 SIG经纬与京新基金是股东
3D content generation based on nerf
处理链中断后如何继续/子链出错removed from scheduling
TPG x AIDU|AI领军人才招募计划进行中!
Cookie
博文推荐|Apache Pulsar 跨地域复制方案选型实践
Differences between MySQL storage engine MyISAM and InnoDB
“新红旗杯”桌面应用创意大赛2022
人均瑞数系列,瑞数 4 代 JS 逆向分析
Creation and assignment of graphic objects
随机推荐
Leetcode skimming: binary tree 23 (mode in binary search tree)
File operation command
Guangzhou held work safety conference
[untitled]
Initialization script
Day22 deadlock, thread communication, singleton mode
企业级自定义表单引擎解决方案(十二)--体验代码目录结构
Creation and assignment of graphic objects
Practical case: using MYCAT to realize read-write separation of MySQL
[untitled]
How to reset Google browser? Google Chrome restore default settings?
AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
Lingyunguang of Dachen and Xiaomi investment is listed: the market value is 15.3 billion, and the machine is implanted into the eyes and brain
What if the xshell evaluation period has expired
API query interface for free mobile phone number ownership
How to continue after handling chain interruption / sub chain error removed from scheduling
test
Find ID value MySQL in string
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~