Logo ROOT   6.10/09
Reference Guide
sqltables.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_sql
3 /// This is an example illustrating how the TSQLFile class can be used.
4 /// Histogram, list of TBox and clones array of TBox objects are stored
5 /// to TSQLFile and read back.
6 /// Except for the specific TSQLFile configuration, the TSQLFile functionality
7 /// is absolutely similar to a normal root TFile
8 ///
9 /// \macro_code
10 ///
11 /// \author Sergey Linev
12 
13 // example configuration for MySQL 4.1
14 const char* dbname = "mysql://host.domain/test";
15 const char* username = "user";
16 const char* userpass = "pass";
17 
18 // example configuration for Oracle 9i
19 //const char* dbname = "oracle://host.domain/db-test";
20 //const char* username = "user";
21 //const char* userpass = "pass";
22 
23 
24 void sqltables()
25 {
26  tables_write();
27  tables_read();
28 }
29 
30 void tables_write()
31 {
32  // first connect to data base
33  // "recreate" option delete all your tables !!!!
34  TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
35  if (f->IsZombie()) { delete f; return; }
36 
37  // you can change configuration only until first object
38  // is written to TSQLFile
40  f->SetArrayLimit(1000);
41  f->SetUseIndexes(1);
42 // f->SetTablesType("ISAM");
43 // f->SetUseTransactions(kFALSE);
44 
45  // lets first write histogram
46  TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
47  h1->FillRandom("gaus",10000);
48  h1->Write("histo");
49  h1->SetDirectory(0);
50 
51  // here we create list of objects and store them as single key
52  // without kSingleKey all TBox objects will appear as separate keys
53  TList* arr = new TList;
54  for(Int_t n=0;n<10;n++) {
55  TBox* b = new TBox(n*10,n*100,n*20,n*200);
56  arr->Add(b, Form("option_%d_option",n));
57  }
58  arr->Write("list",TObject::kSingleKey);
59 
60  // clones array is also stored as single key
61  TClonesArray clones("TBox",10);
62  for(int n=0;n<10;n++)
63  new (clones[n]) TBox(n*10,n*100,n*20,n*200);
64  clones.Write("clones",TObject::kSingleKey);
65 
66  // close connection to database
67  delete f;
68 }
69 
70 
71 void tables_read()
72 {
73  // now open connection to database for read-only
74  TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
75  if (f->IsZombie()) { delete f; return; }
76 
77  // see list of keys
78  f->ls();
79 
80  // get histogram from DB and draw it
81  TH1* h1 = (TH1*) f->Get("histo");
82  if (h1!=0) {
83  h1->SetDirectory(0);
84  h1->Draw();
85  }
86 
87  // get TList with other objects
88  TObject* obj = f->Get("list");
89  cout << "Printout of TList object" << endl;
90  if (obj!=0) obj->Print("*");
91  delete obj;
92 
93  // and get TClonesArray
94  obj = f->Get("clones");
95  cout << "Printout of TClonesArray object" << endl;
96  if (obj!=0) obj->Print("*");
97  delete obj;
98 
99  // this is query to select data of hole class from different tables
100  cout << "================ TBox QUERY ================ " << endl;
101  cout << f->MakeSelectQuery(TBox::Class()) << endl;
102  cout << "================ END of TBox QUERY ================ " << endl;
103 
104  cout << "================== TH1I QUERY ================ " << endl;
105  cout << f->MakeSelectQuery(TH1I::Class()) << endl;
106  cout << "================ END of TH1I QUERY ================ " << endl;
107 
108  // close connection to database
109  delete f;
110 }
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:778
THist< 1, int, THistStatContent > TH1I
Definition: THist.hxx:313
virtual void SetDirectory(TDirectory *dir)
By default when an histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8053
Create a Box.
Definition: TBox.h:24
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
int Int_t
Definition: RtypesCore.h:41
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:543
void Class()
Definition: Class.C:29
TH1F * h1
Definition: legend1.C:5
A doubly linked list.
Definition: TList.h:43
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3294
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:30
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
tomato 1-D histogram with an int per channel (see TH1 documentation)}
Definition: TH1.h:510
char * Form(const char *fmt,...)
void SetUseIndexes(Int_t use_type=kIndexesBasic)
Specify usage of indexes for data tables Index Description kIndexesNone = 0 no indexes are used kInd...
Definition: TSQLFile.cxx:642
TString MakeSelectQuery(TClass *cl)
Produce SELECT statement which can be used to get all data of class cl in one SELECT statement...
Definition: TSQLFile.cxx:1245
write collection with single key
Definition: TObject.h:78
const Bool_t kFALSE
Definition: RtypesCore.h:92
void SetArrayLimit(Int_t limit=20)
Defines maximum number of columns for array representation If array size bigger than limit...
Definition: TSQLFile.cxx:543
double f(double x)
Bool_t IsZombie() const
Definition: TObject.h:122
The TH1 histogram class.
Definition: TH1.h:56
Mother of all ROOT objects.
Definition: TObject.h:37
An array of clone (identical) objects.
Definition: TClonesArray.h:32
virtual void Add(TObject *obj)
Definition: TList.h:77
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
void SetUseSuffixes(Bool_t on=kTRUE)
enable/disable uasge of suffixes in columns names can be changed before first object is saved into fi...
Definition: TSQLFile.cxx:527
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1371
const Int_t n
Definition: legend1.C:16
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.