dyn3.e 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Program type: Embedded Dynamic SQL
  3. *
  4. * Description:
  5. * This program displays employee names and phone extensions.
  6. *
  7. * It allocates an output SQLDA, declares and opens a cursor,
  8. * and loops fetching multiple rows.
  9. * The contents of this file are subject to the Interbase Public
  10. * License Version 1.0 (the "License"); you may not use this file
  11. * except in compliance with the License. You may obtain a copy
  12. * of the License at http://www.Inprise.com/IPL.html
  13. *
  14. * Software distributed under the License is distributed on an
  15. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  16. * or implied. See the License for the specific language governing
  17. * rights and limitations under the License.
  18. *
  19. * The Original Code was created by Inprise Corporation
  20. * and its predecessors. Portions created by Inprise Corporation are
  21. * Copyright (C) Inprise Corporation.
  22. *
  23. * All Rights Reserved.
  24. * Contributor(s): ______________________________________.
  25. */
  26. #include "example.h"
  27. #include <stdlib.h>
  28. #include <string.h>
  29. void print_error (void);
  30. char *sel_str =
  31. "SELECT last_name, first_name, phone_ext FROM phone_list \
  32. WHERE location = 'Monterey' ORDER BY last_name, first_name;";
  33. /* This macro is used to declare structures representing SQL VARCHAR types */
  34. #define SQL_VARCHAR(len) struct {short vary_length; char vary_string[(len)+1];}
  35. char Db_name[128];
  36. EXEC SQL
  37. SET DATABASE empdb = "employee.fdb" RUNTIME :Db_name;
  38. int main(int argc, char** argv)
  39. {
  40. SQL_VARCHAR(15) first_name;
  41. SQL_VARCHAR(20) last_name;
  42. char phone_ext[6];
  43. XSQLDA *sqlda;
  44. short flag0 = 0, flag1 = 0, flag2 = 0;
  45. if (argc > 1)
  46. strcpy(Db_name, argv[1]);
  47. else
  48. strcpy(Db_name, "employee.fdb");
  49. EXEC SQL
  50. WHENEVER SQLERROR GO TO Error;
  51. EXEC SQL
  52. CONNECT empdb;
  53. EXEC SQL
  54. SET TRANSACTION;
  55. /* Allocate an output SQLDA. */
  56. sqlda = (XSQLDA *) malloc(XSQLDA_LENGTH(3));
  57. sqlda->sqln = 3;
  58. sqlda->version = 1;
  59. /* Prepare the query. */
  60. EXEC SQL
  61. PREPARE q INTO SQL DESCRIPTOR sqlda FROM :sel_str;
  62. /*
  63. * Although, all three selected columns are of type varchar, the
  64. * third field's type is changed and printed as type TEXT.
  65. */
  66. sqlda->sqlvar[0].sqldata = (char *)&last_name;
  67. sqlda->sqlvar[0].sqltype = SQL_VARYING + 1;
  68. sqlda->sqlvar[0].sqlind = &flag0;
  69. sqlda->sqlvar[1].sqldata = (char *)&first_name;
  70. sqlda->sqlvar[1].sqltype = SQL_VARYING + 1;
  71. sqlda->sqlvar[1].sqlind = &flag1;
  72. sqlda->sqlvar[2].sqldata = phone_ext;
  73. sqlda->sqlvar[2].sqltype = SQL_TEXT + 1;
  74. sqlda->sqlvar[2].sqlind = &flag2;
  75. /* Declare the cursor for the prepared query. */
  76. EXEC SQL
  77. DECLARE s CURSOR FOR q;
  78. EXEC SQL
  79. OPEN s;
  80. printf("\n%-20s %-15s %-10s\n\n", "LAST NAME", "FIRST NAME", "EXTENSION");
  81. /*
  82. * Fetch and print the records.
  83. */
  84. while (SQLCODE == 0)
  85. {
  86. EXEC SQL
  87. FETCH s USING SQL DESCRIPTOR sqlda;
  88. if (SQLCODE == 100)
  89. break;
  90. printf("%-20.*s ", last_name.vary_length, last_name.vary_string);
  91. printf("%-15.*s ", first_name.vary_length, first_name.vary_string);
  92. phone_ext[sqlda->sqlvar[2].sqllen] = '\0';
  93. printf("%-10s\n", phone_ext);
  94. }
  95. EXEC SQL
  96. CLOSE s;
  97. EXEC SQL
  98. COMMIT;
  99. EXEC SQL
  100. DISCONNECT empdb;
  101. free( sqlda);
  102. return(0);
  103. Error:
  104. print_error();
  105. }
  106. void print_error (void)
  107. {
  108. isc_print_status(gds__status);
  109. printf("SQLCODE=%d\n", SQLCODE);
  110. }