2

We are using ECPG and host variables to connect to a postgres database. We're trying to understand when to use char[] vs VARCHAR[] as our host binding variable. The documentation doesn't provide any pros/cons or use-cases.

For example:

Given column

x VARCHAR (10)

Why would I use

EXEC SQL BEGIN DECLARE SECTION;
 char theX[10];
EXEC SQL END DECLARE SECTION;
cout << theX;

vs. say

EXEC SQL BEGIN DECLARE SECTION;
 VARCHAR theX[10];
EXEC SQL END DECLARE SECTION;
cout << theX.arr;

Thanks!

https://www.postgresql.org/docs/current/ecpg-variables.html

Stanton
  • 497
  • 3
  • 14
  • Of course the documentation does not have anything as what to use depends completely on your intended use. The main difference is char[] defines an array which can contain many values, varchar defined a scalar which contains a single value. Besides the dictum: Never use *char* data type. In this case use varchar[] if you want multiple values. – Belayer Mar 16 '21 at 18:06
  • This doesn't answer my question. I've updated the question to be very specific. Thanks – Stanton Mar 16 '21 at 18:27
  • 1
    I *think* that link refers to database types? I'm concerned with host variables. – Stanton Mar 16 '21 at 19:06
  • @a_horse_without_name The type of your C variable is different from the database data type. – Laurenz Albe Mar 17 '21 at 07:45

1 Answers1

1

It does not matter which type you use in C. As the documentation describes, the difference is that VARCHAR is a struct that also contains the length of the string, while char is the normal null-terminated C string.

If you need the length, VARCHAR might be more convenient.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263