Pro * C simple sample code

Source: Internet
Author: User

The following code connects to the database and then performs some operations

The name of my database table is ym_test. There are only two fields, int type I, and char * type

Dbop. PC File

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include "dbop. H"
  5. Exec SQL begin declare section;
  6. SQL _context CTX;
  7. Exec SQL end declare section;
  8. Exec SQL include sqlca;
  9. Void sqlerror ();
  10. Int conndb (char * sdbconnstr)
  11. {
  12. Exec SQL begin declare section;
  13. Char sconnsql [256];
  14. Exec SQL end declare section;
  15. Memset (sconnsql, 0, sizeof (sconnsql ));
  16. Snprintf (sconnsql, sizeof (sconnsql), "% s", sdbconnstr );
  17. Exec SQL context allocate: CTX;
  18. Exec SQL context use: CTX;
  19. Exec SQL CONNECT: sconnsql;
  20. Return sqlca. sqlcode;
  21. }
  22. Int releasedb ()
  23. {
  24. Exec SQL commit release;
  25. Return 0;
  26. }
  27. Int readdb (INT number)
  28. {
  29. Exec SQL begin declare section;
  30. Int num;
  31. Char name [20];
  32. Exec SQL end declare section;
  33. Num = number;
  34. Exec SQL select Str
  35. Into: Name
  36. From ym_test
  37. Where I =: num;
  38. Printf ("/n % s/n", name );
  39. Return 0;
  40. }
  41. Int writedb (INT number, char * names)
  42. {
  43. Exec SQL begin declare section;
  44. Int num;
  45. Char name [20];
  46. Exec SQL end declare section;
  47. Num = number;
  48. Strcpy (name, names );
  49. Exec SQL insert
  50. Into ym_test (
  51. I,
  52. Str
  53. ) Values (
  54. : Num,
  55. : Name
  56. );
  57. Return 0;
  58. }
  59. Int deletedb (INT number)
  60. {
  61. Exec SQL begin declare section;
  62. Int num;
  63. Exec SQL end declare section;
  64. Num = number;
  65. Exec SQL Delete from ym_test
  66. Where I =: num;
  67. Return 0;
  68. }
  69. Void sqlerror ()
  70. {
  71. Exec SQL whenever sqlerror continue;
  72. Printf ("/noracle error detected:/N ");
  73. Printf ("% d/N", sqlca. sqlcode );
  74. Printf ("/n % s/n", sqlca. sqlerrm. sqlerrmc );
  75. /* Roll back the transaction */
  76. Exec SQL rollback release;
  77. Exit (1 );
  78. }

The main. cpp file code is as follows:

  1. # Include <stdio. h>
  2. # Include <stdlib. h>
  3. # Include <string. h>
  4. # Include "dbop. H"
  5. Int main (INT argc, char * argv [])
  6. {
  7. Conndb (user01/user001 @ testdb );
  8. Readdb (2 );
  9. Readdb (1 );
  10. Writedb (22, "mengge ");
  11. Readdb (22 );
  12. Deletedb (2 );
  13. Releasedb ();
  14. Return 0;
  15. }

 

The compilation script is:

  1. #! /Bin/sh
  2. Proc code = CPP ltype = none INAME = dbop. PC oname = dbop. cpp
  3. G ++-g-wall-C dbop. cpp
  4. G ++-g-wall-C main. cpp
  5. G ++-g-wall-L $ ORACLE_HOME/lib-l clntsh main. O dbop. O-o main
  6. # Deleting intermediate files
  7. Rm-F dbop. O dbop. cpp main. o

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.