当前位置:网站首页>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:创建表
边栏推荐
- 【Presto Profile系列】Timeline使用
- 达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
- 基于鲲鹏原生安全,打造安全可信的计算平台
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- AUTOCAD——大于180度的角度标注、CAD直径符号怎么输入?
- TPG x AIDU|AI领军人才招募计划进行中!
- [untitled]
- Leetcode question brushing: binary tree 26 (insertion operation in binary search tree)
- JS中为什么基础数据类型可以调用方法
- . Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
猜你喜欢
日本政企员工喝醉丢失46万信息U盘,公开道歉又透露密码规则
2022 polymerization process test question simulation test question bank and online simulation test
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
【无标题】
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Leetcode brush questions: binary tree 19 (merge binary tree)
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
3D content generation based on nerf
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
达晨与小米投的凌云光上市:市值153亿 为机器植入眼睛和大脑
随机推荐
.Net下极限生产力之efcore分表分库全自动化迁移CodeFirst
Enterprise custom form engine solution (XII) -- experience code directory structure
红杉中国完成新一期90亿美元基金募集
HZOJ #235. Recursive implementation of exponential enumeration
How to reset Google browser? Google Chrome restore default settings?
Query whether a field has an index with MySQL
有什么类方法或是函数可以查看某个项目的Laravel版本的?
《ASP.NET Core 6框架揭秘》样章[200页/5章]
Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
MySQL importing SQL files and common commands
[untitled]
About the problem of APP flash back after appium starts the app - (solved)
学习突围2 - 关于高效学习的方法
【无标题】
Test next summary
博文推荐|Apache Pulsar 跨地域复制方案选型实践
Four functions of opencv
Day22 deadlock, thread communication, singleton mode
PHP calls the pure IP database to return the specific address
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst