Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCTestInt.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCTestInt.C
1 
2 #include <TError.h>
3 #include <TString.h>
4 #include <TFile.h>
5 #include <TF1.h>
7 #include <RDBC/TSQLConnection.h>
9 #include <RDBC/TSQLResultSet.h>
13 #include <RDBC/TSQLTypes.h>
14 extern "C" {
15 #include <stdlib.h>
16 };
17 //___________________________________________________________________
18 Int_t RDBCTestInt(const Text_t* dsn,
19  const Text_t* usr="",
20  const Text_t* pwd="")
21 {
22  // Open a connection...
23  TSQLConnection* myConnection = NULL;
24  if(usr!="" && pwd !=""){
25  if(getenv("VERBOSE"))
26  printf( "connecting with: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
27  myConnection = TSQLDriverManager::GetConnection( dsn, usr, pwd );
28  } else{
29  if(getenv("VERBOSE"))
30  printf( "connecting with: dsn= %s \n",dsn);
31  myConnection = TSQLDriverManager::GetConnection( dsn );
32  }
33 
34  if(!myConnection) {
35  printf( "failed to connect: dsn= %s usr=%s pwd=%s\n",dsn,usr,pwd);
36  printf("exiting...\n");
37  return -1; // return on error
38  }else
39  printf("connected!!!\n");
40  TSQLStatement* stmt = myConnection->CreateStatement();
41 
42  stmt->ExecuteUpdate( "drop table int_table" );
43  stmt->ExecuteUpdate( "create table int_table (an_int int not null)" );
44  TSQLPreparedStatement* pstmt =
45  myConnection->PrepareStatement("insert into int_table (an_int) values(?)");
46 
47  int some_int = 1;
48  pstmt->SetInt(1,some_int);
49  pstmt->ExecuteUpdate("");
50 
51  some_int = 2;
52 
53  pstmt->SetInt(1,some_int);
54  pstmt->ExecuteUpdate("");
55 
56  TSQLResultSet* rs = stmt->ExecuteQuery("select an_int from int_table order by an_int");
57  rs->Next(); // goto the first row
58  int my_int = (int)rs->GetInt(1); // read int from test_table table
59  printf ("retrieved: %d\n",my_int);
60  rs->Next(); // goto the next row
61  my_int = (int)rs->GetInt(1); // read int from test_table table
62  printf ("retrieved: %d\n",my_int);
63 
64 
65  myConnection->Close();
66  return 0;
67 }
68 
69 //___________________________________________________________________
71 {
72  // handle exceptions
73 
74  TString str = e->GetMessage();
75  printf("SQL Error: %s\n",str.Data());
76 }
77 
78 
80 #ifdef STANDALONE
81 
82 #include <TROOT.h>
83 #include <TSystem.h>
84 #include <iostream>
85 
86 //---- Main program ------------------------------------------------------------
87 
88 TROOT root("RDBCTestInt","Test RDBC TSQLDriverManager and TSQLConnection");
89 
90 int main(int argc, char **argv)
91 {
92 
93  gSystem->Load("libRDBC");
94  Int_t ret = -1;
95 
96  if(argc < 2 || argc > 4){
97  printf ("usage: RDBCTestInt dsn [usr] [password]\n");
98  return ret;
99  }
100 
101  if(argc==2)
102  ret=RDBCTestInt(argv[1]);
103  if(argc==3)
104  ret=RDBCTestInt(argv[1],argv[2]);
105  if(argc==4)
106  ret=RDBCTestInt(argv[1],argv[2],argv[3]);
107 
108  return ret;
109 }
110 #endif