How to use embedded sql to define an array
How to use embedded sql to define an array
От:
Jeffrey Graham <jgraham@lincom-asg.com>
Дата:
Hello everyone,
Can someone please tell me how to declare an array of double (float8)
using esql when you
don't know how long the array is until runtime?
I have the code work for a fixed length of 10, but at run-time I might only
put 2 values or there could just as easily be 100 or 200 or 5000!
I tried several variations declaring my array as a pointer, but ecpg did
not like
any of my attempts.
Thanks,
Jeff
void myinsert(const std::vector& myVector) { EXEC SQL BEGIN DECLARE SECTION;
/* this would be ideal but ecpg does not like it */
/* double m_data[myVector.length()]; */
/* This works but length might actually be 1 or 200! */ double m_data[10];
EXEC SQL END DECLARE SECTION;
/* stuff the sql array with data from my stl vector */ for ( unsigned int i=0; i < myVector.length(); i++ ) m_data[i] = myVector[i];
EXEC SQL insert into eraDATA ( data ) VALUES( :m_data ); return sqlOK(); }
Re: How to use embedded sql to define an array
От:
Michael Meskes <meskes@postgresql.org>
Дата:
On Mon, Jun 10, 2002 at 11:07:52AM -0500, Jeffrey Graham wrote:
> Can someone please tell me how to declare an array of double (float8)
> using esql when you
> don't know how long the array is until runtime?
How do you define the attribute in pgsql? I take it you cannot enter a
dimension here either.
>...
> for ( unsigned int i=0; i < myVector.length(); i++ )
> m_data[i] = myVector[i];
>
> EXEC SQL
> insert into eraDATA
> ( data )
> VALUES( :m_data );
> return sqlOK();
If you just need a way to insert the data you could write your data to a
string together with the insert command so that you end up with a
string:
insert into eraDATA ( data ) VALUES('{1,2,3,....}')
This string can be executed using EXECUTE IMMEDIATE.
Or did you mean something else?
Hope this helps.
Michael
--
Michael Meskes
Michael@Fam-Meskes.De
Go SF 49ers! Go Rhein Fire!
Use Debian GNU/Linux! Use PostgreSQL!