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

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

win10安装mysql和c++读取调用举例_第1张图片

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

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

win10安装mysql和c++读取调用举例_第2张图片

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

win10安装mysql和c++读取调用举例_第3张图片

四、键入如下命令初始化并启动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 
#include 
#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依赖库和包含文件拷贝到工程下

win10安装mysql和c++读取调用举例_第4张图片

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

win10安装mysql和c++读取调用举例_第5张图片

 

你可能感兴趣的:(mysql,数据库)