Statemente update sqlite matlab problem
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 = sprintf('UPDATE acquisition_table SET model = ''%s'' WHERE ID = %d',app.model,app.ID)
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 个评论
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!