stat1.e 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Program type: Embedded Static SQL
  3. *
  4. * Description:
  5. * This program performs a simple update to an existing
  6. * table, asks the user whether to save the update, and
  7. * commits or undoes the transaction accordingly.
  8. * The contents of this file are subject to the Interbase Public
  9. * License Version 1.0 (the "License"); you may not use this file
  10. * except in compliance with the License. You may obtain a copy
  11. * of the License at http://www.Inprise.com/IPL.html
  12. *
  13. * Software distributed under the License is distributed on an
  14. * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
  15. * or implied. See the License for the specific language governing
  16. * rights and limitations under the License.
  17. *
  18. * The Original Code was created by Inprise Corporation
  19. * and its predecessors. Portions created by Inprise Corporation are
  20. * Copyright (C) Inprise Corporation.
  21. *
  22. * All Rights Reserved.
  23. * Contributor(s): ______________________________________.
  24. */
  25. #include "example.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. int do_save (void);
  29. void clean_up (void);
  30. EXEC SQL
  31. BEGIN DECLARE SECTION;
  32. EXEC SQL
  33. END DECLARE SECTION;
  34. int main (void)
  35. {
  36. clean_up();
  37. /* Insert a new row. */
  38. EXEC SQL
  39. INSERT INTO country (country, currency)
  40. VALUES ('Mexico', 'Peso');
  41. /* Check the SQLCODE directly */
  42. if (SQLCODE)
  43. {
  44. isc_print_sqlerror((short)SQLCODE, gds__status);
  45. exit(1);
  46. }
  47. printf("\nAdding: country = 'Mexico', currency = 'Peso'\n\n");
  48. /* Confirm whether to commit the update. */
  49. if (do_save())
  50. {
  51. EXEC SQL
  52. COMMIT RELEASE;
  53. printf("\nSAVED.\n\n");
  54. }
  55. else
  56. {
  57. EXEC SQL
  58. ROLLBACK RELEASE;
  59. printf("\nUNDONE.\n\n");
  60. }
  61. return 0;
  62. }
  63. /*
  64. * Ask the user whether to save the newly added row.
  65. */
  66. int do_save (void)
  67. {
  68. char answer[10];
  69. printf("Save? Enter 'y' for yes, 'n' for no: ");
  70. gets(answer);
  71. return (*answer == 'y' ? 1 : 0);
  72. }
  73. /*
  74. * If this is not the first time this program is run,
  75. * the example row may already exist -- delete the example
  76. * row in order to avoid a duplicate value error.
  77. */
  78. void clean_up (void)
  79. {
  80. EXEC SQL
  81. DELETE FROM country
  82. WHERE country = 'Mexico';
  83. EXEC SQL
  84. COMMIT WORK;
  85. }