mysql odb驱动_odb C++访问mysql数据库,从安装到写入

一:ubuntu下odb 安装

get-apt install gcc

get-apt install g++

get-apt install odb

生成的odb位于:/usr/odb

生成的库(libodb-2.4.so、libodb-mysql-2.4.so)位于:/usr/lib/x86_64-linux-gnu/

二:windows下mysql 8.0的安装

1)下载网址https://dev.mysql.com/downloads/mysql/

37b0f63edf7ea313927fe35657c2ba60.png

用zip形式安装,不要用mysql installer,mysql installer安装就是坑。

2)在下载的mysql-8.0.22-winx64文件夹下面新建一个my.ini文件,内容如下

[mysql]

# 设置mysql客户端默认字符集

default-character-set=utf8

[mysqld]

#设置3306端口

port = 3306

# 设置mysql的安装目录

basedir=F:\mysql\mysql-5.7.24-winx64\mysql-5.7.24-winx64

# 设置mysql数据库的数据的存放目录

datadir=F:\mysql\mysql-5.7.24-winx64\mysql-5.7.24-winx64\data

# 允许最大连接数

max_connections=200

# 服务端使用的字符集默认为8比特编码的latin1字符集

character-set-server=utf8

# 创建新表时将使用的默认存储引擎

default-storage-engine=INNODB1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

3)配置系统环境变量

计算机->右键属性->高级系统设置->高级:环境变量, 添加用户变量MYSQL,在系统变量path后加%MYSQL%

48a6d2b7471ec466d9b4d7788b29ad4b.png

4)以管理员的身份打开cmd窗口跳转路径到D:\mysql\mysql-8.0.22-winx64\bin

初始化8.0版本没有默认密码,初始化命令>mysqld --initialize --user=mysql --console

执行完后,会看见初始密码

安装,输入>mysqld -install

启动服务器,输入>net start mysql

登录,输入>mysql -u root -p

会提示输入密码,输入初始密码

修改密码,输入 mysql>ALTER USER root@localhost IDENTIFIED BY ‘123456’;

密码修改为123456

注释:如果没有记住初始密码,或者忘了,把D:\mysql\mysql-8.0.22-winx64下的data文件夹删了,重写执行以上1-5重来一次

三:navicat链接mysql

遇到Navicat连接MySQL8+时出现2059报错

原因:新版本的MySQL使用的是caching_sha2_password验证方式,但此时的navicat还没有支持这种验证方式。

解决方法:在命令行中登录数据库,执行下面的命令

ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘123456’;

navicat重新登录链接测试成功

7ee251fe27282c3eee4240b992aad523.png

四:通过odb将C++类对象写入数据库

Ubuntu,qt编成环境

1).pro

TEMPLATE = app

CONFIG += console c++11

CONFIG -= app_bundle

CONFIG -= qt

SOURCES += \ main.cpp \ person.cpp \ person-odb.cxx

HEADERS += \ person.h \ person-odb.hxx

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lodb-2.4

INCLUDEPATH += $$PWD/../../../../usr/lib/x86_64-linux-gnu

DEPENDPATH += $$PWD/../../../../usr/lib/x86_64-linux-gnu

unix:!macx: LIBS += -L$$PWD/../../../../usr/lib/x86_64-linux-gnu/ -lodb-mysql-2.4

INCLUDEPATH += $$PWD/../../../../usr/lib/x86_64-linux-gnu

DEPENDPATH += $$PWD/../../../../usr/lib/x86_64-linux-gnu1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

2)person.h

#ifndef PERSON_H

#define PERSON_H

#include

#include

class person

{

public: person (); const std::string& get_email () const; void set_email (const std::string& A); const std::string& get_name () const; void set_name (const std::string& A); unsigned short get_Age () const; void set_Age (unsigned short a);

private: friend class odb::access; std::string email_; std::string name_; unsigned short age_;

};

#pragma db object(person)

#pragma db member(person::email_) id

#endif// PERSON_H1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

3)person.cpp

#include "person.h"

person::person()

{

}

const std::string& person::get_email () const

{ return email_;

}

void person::set_email (const std::string& A)

{ email_ = A;

}

const std::string& person::get_name () const

{ return name_;

}

void person::set_name (const std::string& A)

{ name_ = A;

}

unsigned short person::get_Age () const

{ return age_;

}

void person::set_Age (unsigned short a)

{ age_ = a;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

4)main.cpp文件

#include

using namespace std;

#include// std::auto_ptr

#include

#include

#include

#include

#include "person.h"

#include "person-odb.hxx"

using namespace std;

using namespace odb::core;

int main(int argc, char* argv[])

{ try { auto_ptr<:database> db ( new odb::mysql::database ( "root" // database login name ,"123456" // database password ,"person" // database name ,"127.0.0.1" ,3306 )); std::string john_id, jane_id, joe_id; person john;// ("John", "Doe", 33); john.set_name("John"); john.set_email("Doe"); john.set_Age(33); person jane;// ("Jane", "Doe", 32); jane.set_name("Jane"); jane.set_email("Doee"); jane.set_Age(32); person joe;// ("Joe", "Dirt", 30); joe.set_name("Joe"); joe.set_email("Dirt"); joe.set_Age(30); transaction t (db->begin ()); john_id = db->persist (john); jane_id = db->persist (jane); joe_id = db->persist (joe); t.commit (); } catch (const odb::exception& e) { cerr << e.what () << endl; return 1; } return 0;

}1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

现在是还不能运行滴,编译都编译不过

5)编译生成 :person-odb.hxx、person-odb.cxx、person-odb.ixx、person.sql

在person-odb.h所在目录下打开ubuntu命令窗口执行

odb -d mysql --generate-query --generate-schema person.h

注释(网上看见的例子是.hxx, 实际上我们一般写.h多一点,.hxx和.h生成dob文件是一样)

6)执行person.sql正常数据库表

在数据库中创建person数据库

将person.sql放到E:

windows的cmd.exe下执行

mysql –u用户名 –p密码 –D数据库

>mysql –u root –p123456 -Dperson

2

c99069941d6b4193d030aa263e2d4d91.png

7)编译刚刚的qt工程,能正常编译通过,运行程序,正常将数据写入mysql库中

8b0875c8847e2aea90ad7831d2fcb42c.png

文章来源: blog.csdn.net,作者:岑小岑,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/cxd1314520/article/details/110129402

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值