2

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.

7
  • 1
    It doesn't necessarily have to be closed. It may help someone else who comes here with the same problem. Commented Dec 21, 2009 at 17:39
  • on an unrelated note, I once used pgmex library to interface with PostgreSQL from MATLAB (uses the libpq C library directly): dertech.com/pgmex/pgmex.html Commented Dec 21, 2009 at 18:54
  • but the built-in functions to communicate matlab with postgres are quite good, is there any significant difference in using pgmex? Commented Dec 21, 2009 at 20:21
  • the built-ins connect using ODBC, as opposed to using the native C API functions though mex. Therefore its a performance issue since you can think of ODBC as a middleware layer... Commented Dec 21, 2009 at 21:29
  • 2
    did you add the corresponding JDBC jar file to the javaclasspath? you can get the latest PostgreSQL JDBC driver at jdbc.postgresql.org Commented Dec 21, 2009 at 23:12

1 Answer 1

4

Have you tried using single quotes?

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

q =

insert into test(name) values('xxx')
Sign up to request clarification or add additional context in comments.

1 Comment

Yup, I came up with the same thing. Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.