Getting Error while assigning variable to MySQL database

조회 수: 10 (최근 30일)
Sudharsan K.R
Sudharsan K.R 2019년 3월 28일
답변: Piyush Kumar 2024년 9월 30일
I am trying to execute the below query and getting the below error. Kindly help me out
datasource = 'MY SQL Server Auth';
conn = database('MYDB','user','pwd');
var= "number";
selectquery = ['Select * from TABLE2 where Sensor_value = ' var];
data = fetch(conn,selectquery)
close(conn)
Error:-
Error using database.odbc.connection/fetch (line 39)
ODBC Driver Error: [MySQL][ODBC 8.0(a) Driver][mysqld-5.7.25-0ubuntu0.18.04.2]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 '&' at line 1

답변 (1개)

Piyush Kumar
Piyush Kumar 2024년 9월 30일
The issue is with the way the query string is constructed.
var= "number";
selectquery = ['Select * from TABLE2 where Sensor_value = ' var];
disp(selectquery)
"Select * from TABLE2 where Sensor_value = " "number"
To fix this, ensure that the variable is correctly concatenated into the SQL query string. In MATLAB, you can use "sprintf" -
selectquery = sprintf('SELECT * FROM TABLE2 WHERE Sensor_value = "%s"', var);
disp(selectquery)
SELECT * FROM TABLE2 WHERE Sensor_value = "number"

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by