rust实现一个mysql驱动,1. [Rust] diesel支持mysql方法

如果使用rocket框架,那么就不需要往下看了。因为rocket框架已经内置了ORM

配置diesel遇到了N个坑!在这里记录一下!我的电脑是win10 64位。diesel是数据库持久层。

开始操作

找到mysql-connector-c安装路径, 我的路径如下 C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14

以管理员权限启动CMD, 执行如下命令

setx MYSQLCLIENT_LIB_DIR "C:\Program Files\MySQL\MySQL Connector C 6.1\lib\vs14"

配置config文件,文件路径为 C:\Users\用户名\.cargo\config,如果不存在那就创建config文件,内容如下。

#配置使用中科大源

[http]

check-revoke = false

[source.crates-io]

replace-with = 'ustc'

[source.ustc]

registry = "https://mirrors.ustc.edu.cn/crates.io-index"

#配置mysql编译

[build]

rustflags = ["-L", "C:\\Program Files\\MySQL\\MySQL Connector C 6.1\\lib\\vs14"]

在上面的配置文件中有[build]配置内容,这一步非常重要,如果在这里不进行配置的话,即使是diesel安装成功了,但是项目启动时无法正常启动,原因是在编译、测试时还是要这个环境变量,因为其中的依赖diesel = { version = "1.4.5", features = ["mysql"] }需要它

安装diesel

cargo install diesel_cli --no-default-features --features mysql

安装完成后执行diesel,结果如下。

C:\WINDOWS\system32>diesel

diesel 1.4.1

USAGE:

diesel [FLAGS] [OPTIONS]

FLAGS:

--locked-schema Require that the schema file is up to date

-h, --help Prints help information

-V, --version Prints version information

OPTIONS:

--config-file The location of the configuration file to use. Falls back to the

`DIESEL_CONFIG_FILE` environment variable if unspecified. Defaults to

`diesel.toml` in your project root. See diesel.rs/guides/configuring-diesel-cli

for documentation on this file.

--database-url Specifies the database URL to connect to. Falls back to the DATABASE_URL

environment variable if unspecified.

SUBCOMMANDS:

bash-completion DEPRECATED: Generate bash completion script for the diesel command.

completions Generate shell completion scripts for the diesel command.

database A group of commands for setting up and resetting your database.

help Prints this message or the help of the given subcommand(s)

migration A group of commands for generating, running, and reverting migrations.

print-schema Print table definitions for database schema.

setup Creates the migrations directory, creates the database specified in your DATABASE_URL, and

runs existing migrations.

You can also run `diesel SUBCOMMAND -h` to get more information about that subcommand.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值