views:

182

answers:

1

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

There is no difference if I use "%s" format or plain %s. Where is the problem?

EDIT

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.

+3  A: 

Have you tried using single quotes?

>> q = sprintf('insert into test(name) values(''%s'')', 'xxx')

q =

insert into test(name) values('xxx')
gnovice
Yup, I came up with the same thing. Thanks for the answer.
Gacek