Statemente update sqlite matlab problem

조회 수: 6 (최근 30일)
Massimo
Massimo 2023년 6월 27일
댓글: Rik 2023년 7월 28일
Hello guys, I'm trying to use the command execute to make an update to a sqlite database. I'm on 2022b version, so I can't use sqlupdate command.
If I write this code it works:
statementUpdate = ["UPDATE acquisition_table SET model = '" + app.model + "' WHERE ID = 3 "];
execute(conn,statementUpdate) obviously after having estabilished a connection with my database.
If you notice, in the the statement there is app.model. It is a variable which is updated trhough app designer (model is a property) and so I can write that value modified in my database. I'd like to do the same with ID, which I also have set like a property, but I don't know how to modify that statement. It's probably something related to the position of quotes or the apostrophe, I don't know. I need this because that ID is a filter I use to enter in my database, and it changes when I do some stuffs in my app desinger interface. So having it like a fixed value (WHERE ID = 3 for example) is completely useless for me.
Edit: I forgot to say that statement has been suggested to me, so I have doubt also about the use of + symbol
Thanks guys.

채택된 답변

Rik
Rik 2023년 6월 27일
This seems a case where you want to compose a string from several elements.
You can do this with the string datatype (as you have done here). The benefit is that this will automagically convert numbers to string before concatenating the elements if you use +, while char will be treated as a number if you use +.
Another option is to move to sprintf. That way you have control over the exact format used.
app.model = "some_name";
app.ID = 3;
statementUpdate = "UPDATE acquisition_table SET model = '" + app.model + "' WHERE ID = 3 "
statementUpdate = "UPDATE acquisition_table SET model = 'some_name' WHERE ID = 3 "
statementUpdate = sprintf('UPDATE acquisition_table SET model = ''%s'' WHERE ID = %d',app.model,app.ID)
statementUpdate = 'UPDATE acquisition_table SET model = 'some_name' WHERE ID = 3'
As you can see, both return the same thing, just a different datatype.
One last thing: I thought you should provide the SQLite string datatype with " instead of ' (and that you need to finish every statement with a semicolon). That might cause the underlying problem with this statement.
statementUpdate = sprintf('UPDATE acquisition_table SET model = "%s" WHERE ID = %d;',app.model,app.ID)
  댓글 수: 9
Massimo
Massimo 2023년 7월 27일
You're right.
First of all, I managed to write a statement which the fetch command can execute. I write you it:
sqlquery = ['Select * FROM acquisition_table WHERE Type_of_maneuvers = ''', app.ManeuvValue,''''];
app.ManeuValue it's a property I set on app designer, so that I can update its value if necessary. What I'm trying to do now (and I don't know if it's possible), is to concatenate more infos inside sqlquery statement. What I need is to specify not just one WHERE information, but more than one.
With the UPDATE statement I could write this to concatenate more SET command:
"UPDATE acquisition_table SET mat_dynamics_RAW = '" + app.MatDynamicsRaw + "',mat_dynamics_Translated = '" + app.MatDynamicsTranslated + "' WHERE ID = " + app.filter.Var1;
Unfortunately I'm not able to do the same thing inside my sqlquery I wrote you at the beginning of this anwser.
Thanks @Rik
Rik
Rik 2023년 7월 28일
This sounds more like a question about SQLite syntax. What is the actual text you want to compose? Does SQLite even support what you want to do?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by