【MySQL案例】ERROR 1418

1.1.1. ERROR 1418

【环境描述】

mysql5.0.67

【问题描述】

创建存储过程的时候遇到ERROR 1418报错。

# 创建函数的SQL语句

CREATE FUNCTION `xxx`( num01 int ) RETURNSint(11)

begin

       declare mm  int default 0;

       if  (xx) then

                set mm = num01*num01*10;

       elseif  xxx then

                set mm = 10;  

       else

                case num01

                        when  30 then set mm = 111;

                        when  31 then set mm = 222;

                end case;      

       end if;

       return mm;

end

 

# 报错信息

# ERROR 1418 (HY000) at line xxxxx: Thisfunction has none of DETERMINISTIC, NO SQL, or READS SQL DATA in itsdeclaration and binary logging is enabled (you *might* want to use the lesssafe log_bin_trust_function_creators variable)

 

【解决方法】

关闭关闭binary log或者修改参数。

此处采用修改参数的方法:

set globallog_bin_trust_function_creators=1;

 

【报错原因】

By default, for a CREATE FUNCTION statement to be accepted, at least one of DETERMINISTIC, NOSQL, or READS SQL DATA must be specified explicitly. Otherwise an error occurs:

ERROR1418 (HY000): This function has none of DETERMINISTIC, NO SQL,

or READSSQL DATA in its declaration and binary logging is enabled

(you*might* want to use the less safe log_bin_trust_function_creators

variable)

If you set log_bin_trust_function_creators to 1, the requirement that functions be

deterministic or not modify data is dropped.

 

【参考资料】

Command-LineFormat --log-bin-trust-function-creators

Option-FileFormat log-bin-trust-function-creators

SystemVariable Name log_bin_trust_function_creators

VariableScope Global

DynamicVariable Yes

PermittedValues

Type boolean

Default FALSE

 

This variable applieswhen binary logging is enabled. It controls whether stored function creators canbe trusted not to create stored functions that will cause unsafe events to bewritten to the binary log. If set to 0 (the default), users are not permittedto create or alter stored functions unless they have the SUPER privilege in addition to the CREATE ROUTINE or ALTER ROUTINE privilege. A setting of0 also enforces the restriction that a function must be declared with the DETERMINISTIC characteristic, or with the READS SQL DATA or NO SQL characteristic. If the variable is set to 1, MySQLdoes not enforce these restrictions on stored function creation. This variablealso applies to trigger creation. See Section19.7, “Binary Logging of Stored Programs”.

以下是一个简单的代码示例,展示如何使用WebSocket与MySQL进行交互: server.js ```javascript const WebSocket = require('ws'); const mysql = require('mysql'); const dbConnection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); dbConnection.connect(); const server = new WebSocket.Server({ port: 8080 }); server.on('connection', (socket) => { console.log('Client connected.'); socket.on('message', (message) => { console.log(`Received message: ${message}`); const query = `SELECT * FROM mytable WHERE id = ${message}`; dbConnection.query(query, (error, results, fields) => { if (error) { socket.send(JSON.stringify({ error: error })); } else { socket.send(JSON.stringify({ data: results })); } }); }); socket.on('close', () => { console.log('Client disconnected.'); }); }); ``` client.html ```html <!DOCTYPE html> <html> <head> <title>WebSocket and MySQL Demo</title> </head> <body> <h1>WebSocket and MySQL Demo</h1> <label for="id-input">Enter ID:</label> <input id="id-input" type="text"> <button id="query-button">Query</button> <p id="result"></p> <script> const socket = new WebSocket('ws://localhost:8080'); socket.addEventListener('open', (event) => { console.log('Connected to server.'); }); document.getElementById('query-button').addEventListener('click', () => { const id = document.getElementById('id-input').value; socket.send(id); }); socket.addEventListener('message', (event) => { const data = JSON.parse(event.data); if (data.error) { document.getElementById('result').innerHTML = `Error: ${data.error}`; } else { document.getElementById('result').innerHTML = JSON.stringify(data.data); } }); socket.addEventListener('close', (event) => { console.log('Disconnected from server.'); }); </script> </body> </html> ``` 当客户端连接到服务器时,它会显示一个输入框和一个按钮。当用户点击按钮时,它会发送一个包含所输入ID的WebSocket消息。服务器会将该消息用作查询条件,从MySQL数据库中检索数据,并将结果作为JSON消息发送回客户端。客户端显示结果或错误消息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值