| Linuxコマンド |
| 用語集 |
| debian メモ |
| apache2 メモ |
| MySQL メモ |
| Oracle メモ |
| PostgreSQL |
| HOME |
RustでSQLxクレートを使ってMySQLのデータを検索してみます。
◆環境| OS | Windows 11 Home 25H2 |
|---|---|
| rustc | 1.91.1 |
| cargo | 1.91.1 |
プロジェクトの作成
プロジェクトを作成します。
cargo new sqlxtest2
コマンドプロンプトでプロジェクトを作成したいフォルダへ移動しコマンドを実行します。
> cargo new sqlxtest2
Creating binary (application) `sqlxtest` package
note: see more `Cargo.toml` keys and their definitions at https://doc
.rust-lang.org/cargo/reference/manifest.html
Creating binary (application) `sqlxtest` package
note: see more `Cargo.toml` keys and their definitions at https://doc
.rust-lang.org/cargo/reference/manifest.htmlプログラム作成
ポイントは以下になります。
- コネクションプールを使う。(MySqlPoolOptions::new())
- プリペアドステートメントを使う。(?とbind())
- 接続文字列はハードコーディングせず.envで管理する。
- sqlx::query_as!でコンパイル時にSQLの検証をする。
| Cargo.toml | ||
|
| main.rs | ||
|
DBはDocker上に構築していてlocalhost:13306で待ち受けているのでポートは13306にしました。
docker container restart mysql-container-8.0.41
| .env | ||
|
テーブルとテストデータは MySQLのデータ検索(SQLx) で作成したものを使います。
実行してみます。
PS C:\src\sqlxtest2> cargo run
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: set `DATABASE_URL` to use query macros online, or run `cargo s
qlx prepare` to update the query cache
--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * FROM rec_values WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)
error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous error
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: set `DATABASE_URL` to use query macros online, or run `cargo s
qlx prepare` to update the query cache--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * FROM rec_values WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous errorエラーになりました。DATABASE_URLを指定する必要があるようです。 .envファイルにDATABASE_URLを追加します。
| .env | ||
|
実行してみます。
PS C:\src\sqlxtest2> cargo run
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.24s
Running `target\debug\sqlxtest2.exe`
Hello, world!
mysql://root:root@localhost:13306/MyDB
id=1 date=2026-03-15 09:22:16 value=12.3
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.24s
Running `target\debug\sqlxtest2.exe`
Hello, world!
mysql://root:root@localhost:13306/MyDB
id=1 date=2026-03-15 09:22:16 value=12.3
見た感じは特に変わりがなく期待通りに動作しました。
テーブル名を間違えてみます。
PS C:\src\sqlxtest2> cargo run
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: error returned from database: 1146 (42S02): Table 'MyDB.rec_va
lue' doesn't exist
--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * FROM rec_value WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)
error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous error
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: error returned from database: 1146 (42S02): Table 'MyDB.rec_va
lue' doesn't exist--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * FROM rec_value WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous errorSQL文を間違えてみます。
PS C:\src\sqlxtest2> cargo run
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: error returned from database: 1064 (42000): You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'rec_values WHERE
id=?' at line 1
--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * rec_values WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)
error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous error
Compiling sqlxtest2 v0.1.0 (C:\src\sqlxtest2)
error: error returned from database: 1064 (42000): You have an error
in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near 'rec_values WHERE
id=?' at line 1--> src\main.rs:16:15
|
16 | let rec = sqlx::query_as!(Rec,
| _______________^
17 | | "SELECT * rec_values WHERE id=?",
18 | | id
19 | | )
| |_____^
|
= note: this error originates in the macro `$crate::sqlx_macros::e
xpand_query` which comes from the expansion of the macro `sqlx::que
ry_as` (in Nightly builds, run with -Z macro-backtrace for more inf
o)error: could not compile `sqlxtest2` (bin "sqlxtest2") due to 1 previ
ous error接続文字列はDATABASE_URLがそのままで使えました。
| main.rs | ||
|
SQLをチェックできるのは便利ですね。
関連項目
Docker MySQL
MySQLでテーブル作成、データ追加、検索
MySQLのデータ検索(SQLx)
MySQLのデータ追加(SQLx)
Copyright (C) 2026 ymlib.com
