win10安装mysql和c++读取调用举例

一、下载mysql8.rar解压到C盘(也可以解压到其他位置)

在系统环境变量添加JAVA_HOME=C:\myslq8,并在path中添加%JAVA_HOME%\bin;

二、以管理员身份进入命令窗口

三、修改配置文件指定安装路径和数据库的存放路径

四、键入如下命令初始化并启动mysql服务,然后修改登录密码

C:\>mysqld --initialize --user=mysql --console
C:\>mysqld -install
C:\>net start MySQL
C:\>mysql -u root -p (回车让输入的密码就是第一个命令后出现的临时密码
mysql> ALTER USER root@localhost IDENTIFIED BY "12345678"; (新修改密码自己指定
mysql> flush privileges;
mysql> exit

五、手动命令建数据库的命令如下

C:\> mysql -u root -p
Enter password: ******** (上面自己修改的新密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create testdatabase; (创建的数据库为testdatabase
mysql> use testdatabase;  (切换到刚建的数据库
mysql> create table customers (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(255),address VARCHAR(255),city VARCHAR(255)); (创建表customers
mysql> insert into customers (name,address,city) values('张三','海淀区','北京'); (插入新记录
mysql> select * from customers; (显示新记录
+----+--------+-----------+--------+
| id | name   | address   | city   |
+----+--------+-----------+--------+
|  1 | 张三   | 海淀区    | 北京   |
+----+--------+-----------+--------+
1 row in set (0.00 sec)

六、VS2022调试读取源代码如下

// TestMySQL.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include <iostream>
#include "include/mysql.h"

using namespace std;
#pragma comment(lib,"lib/libmysql.lib")

int main()
{
    MYSQL mysql;
    mysql_init(&mysql);

    if (!(mysql_real_connect(&mysql, "127.0.0.1", "root", "12345678", "testdatabase", 0, NULL, 0)))
    {
        cout << "Connect database,fail!\n : ";
        cout << mysql_error(&mysql);
        exit(-1);
    }

    mysql_query(&mysql, "set names gbk");
    mysql_query(&mysql, "select * from customers where name = '张三'");

    MYSQL_RES* res = mysql_store_result(&mysql);
    if (res == NULL)
    {
        mysql_close(&mysql);
        cout << mysql_error(&mysql);
        exit(-1);
    }

    MYSQL_ROW row;
    int count = mysql_num_fields(res);

    while (row = mysql_fetch_row(res))
    {
        for (int kkk = 0; kkk < count; kkk++)
        {
            cout << row[kkk] << "\t";
        }
        cout << endl;
    }

    mysql_free_result(res);
    mysql_close(&mysql);

    return 0;
}

七、编译时前要把mysql依赖库和包含文件拷贝到工程下

 八、编译后的运行结果如下

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

princewwj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值