views:

270

answers:

3
+2  Q: 

embedded sql in C

I've been attempting to write embedded SQL statements for DB2 that ultimately gets compiled in C.

I couldn't find a tutorial or manual on the embedded SQL syntax for C for reference. One case I would like to do is to insert data into a table. I know most embedded sql statements need the initalizer EXEC SQL, but thats the extent of my knowledge generally. I'm doing this for an assignment and would appreciate if there more information regarding this or solution.

Example of a statement to query the database:

EXEC SQL SELECT SNAME, AGE into :sname, :sage FROM ONE.SAILOR WHERE sid = :sid;

I like to see what statement allows me to INSERT into the database. I've tried something like:

EXEC SQL INSERT ....

but doesn't work.

Thanks.

+2  A: 

See IBM's Embedded SQL manual.

Embedded SQL is largely the same no matter what the host language is.

S.Lott
+1  A: 

I just started using sqllite. Besides the good documentation for C++, SQLlist might be a nice thing to have because you can unit-test your code without being dependent on DB2 and it's really easy add with your code.

David Nehme
+1  A: 

The four dots aren't syntactically valid :-D

The reliable way is the same as with any other INSERT statement: list the columns and the values.

EXEC SQL INSERT INTO SomeTable(Col1, Col2, Col3) VALUES(:hv1, :hv2, :hv3);

Here, the :hv1, :hv2 and :hv3 represent three host variables of types appropriate to the columns in the table. Note that the table could contain other columns than these three as long as those columns have a default specified or accept NULL (which is really just a default default in this case). The unreliable way does not list the columns:

EXEC SQL INSERT INTO SomeTable VALUES(:hv1, :hv2, :hv3);

Now you are dependent on getting the sequence right, and you must provide a value for each column -- there cannot be extra columns in SomeTable.

Jonathan Leffler