I have successfully create a user name = ‘root’ and host = ‘127 .0.0.1 ‘, use the mysql command (client mode) as follow:
sql = String.Format("CREATE USER '{0}'@'{1}' IDENTIFIED BY '{2}'; GRANT ALL PRIVILEGES ON * . * TO '{0}'@'{1}' IDENTIFIED BY '{2}' WITH GRANT OPTION;", userName, serverHost, userPassword)
When the script above I change it to store procedure (server mode) as below, I found the error as follows: (Error 1064 – You have an error in your SQL syntax; check the manual That corresponds to Yout MySQL server version for the right syntax to use near ‘pass’ at line 1)
ScriptExec(String.Format("DROP PROCEDURE IF EXISTS mySchema.SP_CreateUser $$" +
"CREATE DEFINER=root@localhost PROCEDURE {0}.SP_CreateUser(IN userName VARCHAR(250), IN serverHost VARCHAR(250), IN userPassword VARCHAR(250)) " +
"BEGIN " +
"SET @buf = CONCAT('CREATE USER ',userName,'@',serverHost,' IDENTIFIED BY ',userPassword); PREPARE stmt FROM @buf; EXECUTE stmt; DEALLOCATE PREPARE stmt;" +
"SET @buf1 = CONCAT('GRANT ALL PRIVILEGES ON * . * TO ',userName,'@',serverHost,' IDENTIFIED BY ',userPassword,' WITH GRANT OPTION'); PREPARE stmt1 FROM @buf1; EXECUTE stmt1; DEALLOCATE PREPARE stmt1;" +
"END $$"))
Public Function ScriptExec(ByVal sql As String) As Boolean
Dim stsResult As Boolean = Nothing
Dim has As Integer = Nothing
Dim script As New MySqlScript(objCon, sql)
script.Delimiter = "$$"
Try
has = script.Execute()
If has > 0 Then stsResult = True
Catch ex As MySqlException
MessageBox.Show("Error occurred in ScriptExec Function" & vbCrLf & ex.Number & " – " & ex.Message)
Finally
script.Delimiter = ";"
End Try
Return stsResult
End Function
Is there a solution to my problem above?
Thanks & Best Regards,
Yudha
Aren’t you missing a
statement at the beginning of your SQL code? Without it, the engine won’t know how to interpret the
$$.