问题描述
我正在尝试将一些数据从MATLAB导出到数据库.我通过ODBC使用PostgreSQL数据库,如下所示.首先,我创建连接:
I'm trying to export some data from MATLAB to a database. I use a PostgreSQL database via ODBC as follows. First, I create the connection:
dbConn = database('PostgreSQL30', username,password);
如果我尝试手动执行一些测试插入,则一切看起来都很好:
If I try to execute some test insertion manually, everything looks fine:
exec( dbConn, 'insert into test(std) values(2.2)')
当我尝试动态生成一些简短查询时,一切仍然看起来不错:
When I try to generate some short query dynamically, everything still looks fine:
q = sprintf('insert into test(std) values(%2.2f)', 12.345);
res = exec(dbConn, q);
但是当我尝试生成一些包含字符串的查询时,出现错误:
But when I try to generate some query containing strings, I get an error:
>> q = sprintf('insert into test(name) values("%s")', 'xxx')
q =
insert into test(name) values("xxx")
>> res = exec(dbConn, q);
>> res.Message
ans =
ERROR: column "xxx" does not exist;
Error while executing the query
如果我使用"%s"
格式或普通的%s
,没有任何区别.问题出在哪儿?
There is no difference if I use "%s"
format or plain %s
. Where is the problem?
编辑
好的,我使用了错误的引号.当我使用时:
OK, I used the wrong quotation marks. When I use:
q = sprintf('insert into test(name) values(''%s'')', 'xxx')
一切正常.因此,可以关闭/删除问题.抱歉打扰你了.
everything is OK. So the question can be closed/deleted. Sorry to bother you.
推荐答案
您是否尝试过使用单引号?
Have you tried using single quotes?
>> q = sprintf('insert into test(name) values(''%s'')', 'xxx')
q =
insert into test(name) values('xxx')
这篇关于为什么在MATLAB中创建SQL查询时出现此错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!