From ce35cc4451235c00951baeb244a783e767cb636d Mon Sep 17 00:00:00 2001 From: pengchanglu Date: Sat, 28 Sep 2024 17:18:10 +0800 Subject: [PATCH] If you choose SQLx, there will be an error when using MySQL. SQLx official website documentation: // Make a simple query to return the given parameter (use a question mark `?` instead of `$1` for MySQL/MariaDB) --- src/template/src/services/user.hbs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/template/src/services/user.hbs b/src/template/src/services/user.hbs index 1ac5a66..40f311d 100644 --- a/src/template/src/services/user.hbs +++ b/src/template/src/services/user.hbs @@ -35,7 +35,11 @@ pub async fn add_user(req: UserAddRequest) -> AppResult { let _ = sqlx::query!( r#" INSERT INTO users (id, username, password) + {{#if is_mysql}} + VALUES (?, ?, ?) + {{else}} VALUES ($1, $2, $3) + {{/if}} "#, id, req.username, @@ -56,7 +60,11 @@ pub async fn login(req: UserLoginRequest) -> AppResult { User, r#" SELECT id, username, password FROM users + {{#if is_mysql}} + WHERE username = ? + {{else}} WHERE username = $1 + {{/if}} "#, req.username ) @@ -88,8 +96,13 @@ pub async fn update_user(req: UserUpdateRequest) -> AppResult { let _ = sqlx::query!( r#" UPDATE users + {{#if is_mysql}} + SET username = ?, password = ? + WHERE id = ? + {{else}} SET username = $1, password = $2 WHERE id = $3 + {{/if}} "#, req.username, hash_password, @@ -109,7 +122,12 @@ pub async fn delete_user(id: String) -> AppResult<()> { sqlx::query!( r#" DELETE FROM users + {{#if is_mysql}} + WHERE id = ? + {{else}} WHERE id = $1 + {{/if}} + "#, id, )