1

I'm trying to use prepared statements in PostgreSQL, but it's giving me some pretty frustrating errors.

I'm trying to select a single record from the DB, by MediaID. In the DB, MediaID is a Serial Integer. In the class structure, it's an int.

When I call sql.Prepare(), however, it's telling me that I have invalid input syntax for MediaID. I don't see how that could be the case.

NpgsqlCommand sql = mediaRepository.CreateCommand();

sql.CommandText = "SELECT * FROM Media WHERE 'MediaID' = :MediaID";
sql.Parameters.Add(new NpgsqlParameter("MediaID", NpgsqlDbType.Integer));
sql.Prepare();
sql.Parameters["MediaID"].Value = id;

The frustrating part is that if I set miscast the int as NpgsqlDbType.Varchar, it prepares just fine - it just doesn't return any information.

Any ideas?

1 Answer 1

1

Try changing this sentence:

sql.CommandText = "SELECT * FROM Media WHERE 'MediaID' = :MediaID";

Into this:

sql.CommandText = "SELECT * FROM Media WHERE MediaID = :MediaID";

Note the removal on single quoted from the MediaID field.

Sign up to request clarification or add additional context in comments.

4 Comments

When I do that, it can no longer find the coloumn "MediaID".
If you have mixed-case name, quote it but with double quotes not with apostrophes.
Does this mean I should be avoiding mixed case names with PostgreSQL? Is this convention?
This was it! Wow, that's so frustrating. Note to self: Avoid mixed case names in PGSQL.

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.