# MySQL error while using gorm exec

**URL:** <https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989>\
**Category:** Getting Help\
**Created:** [September 11, 2023, 9:11am UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989 "2023-09-11T09:11:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![2paws](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/2/cc9497/32.png) [@2paws](https://forum.golangbridge.org/u/2paws)\
**Post date:** [September 11, 2023, 9:11am UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/1 "2023-09-11T09:11:05Z")

</div>

Hi guys, I’m trying to use gorm to create a new user for my local mysql,it fails when I try to use "?"in gormdb.exec:

```auto
err := db.Exec("CREATE USER ? IDENTIFIED BY ?", a.Name, a.Pwd).Error

```

And it returns error:

```auto
[1.824ms] [rows:0] CREATE USER 'Reiis' IDENTIFIED BY '12345'
Error 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 '? IDENTIFIED BY ?' at line 1

```

Seems like gorm didn’t swap the “?” with a.Name? but it also prints the correct SQL line on the screen.

When I try to run the code below it works:

```auto
cmd := fmt.Sprintf("CREATE USER '%s' IDENTIFIED BY '%s'", a.Name, a.Pwd)
err := db.Exec(cmd).Error

```

It’s just so weird. 😢 Does anyone know what the problem is? Thx!

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [September 11, 2023, 10:09am UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/2 "2023-09-11T10:09:53Z")

</div>

Not everything can be parameterized, and `CREATE USER` is one such example.

Why are you creating database users from your application?

---

<div class="post-metadata">

**Author:** ![2paws](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/2/cc9497/32.png) [@2paws](https://forum.golangbridge.org/u/2paws)\
**Post date:** [September 11, 2023, 11:05am UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/3 "2023-09-11T11:05:50Z")

</div>

So it’s not a bug but a feature? Actually I want to make some kind of control manager over MySQL clusters and using GORM to avoid sql injection. Is there any better ways to realize those features ? Thanks!

---

<div class="post-metadata">

**Author:** ![skillian](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/skillian/32/7876_2.png) [@skillian](https://forum.golangbridge.org/u/skillian)\
**Post date:** [September 11, 2023, 1:22pm UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/4 "2023-09-11T13:22:13Z")

</div>

I don’t believe it’s a feature; just a “limitation.” Maybe that limitation could be considered a bug. I’m not familiar with MySQL, but you might be able to do it indirectly with something like this:

```go
db.Exec(`
    SET @username = ?;
    SET @password = ?;
    SET @sql = CONCAT("CREATE USER ", QUOTE(@username), " IDENTIFIED BY ", QUOTE(@password), ";"); 
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
`, a.Name, a.Pwd)

```

---

<div class="post-metadata">

**Author:** ![2paws](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/2/cc9497/32.png) [@2paws](https://forum.golangbridge.org/u/2paws)\
**Post date:** [September 11, 2023, 1:51pm UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/5 "2023-09-11T13:51:30Z")

</div>

Wow that seems to work. I will try it tomorrow, thanks so much 😄

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 10, 2023, 1:51pm UTC](https://forum.golangbridge.org/t/mysql-error-while-using-gorm-exec/32989/6 "2023-12-10T13:51:44Z")

</div>

Closed on December 10, 2023, 1:51pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
