Logo ROOT  
Reference Guide
sqltables.C File Reference

Detailed Description

This is an example illustrating how the TSQLFile class can be used.

Histogram, list of TBox and clones array of TBox objects are stored to TSQLFile and read back. Except for the specific TSQLFile configuration, the TSQLFile functionality is absolutely similar to a normal root TFile

// example configuration for MySQL 4.1
const char* dbname = "mysql://host.domain/test";
const char* username = "user";
const char* userpass = "pass";
// example configuration for Oracle 9i
//const char* dbname = "oracle://host.domain/db-test";
//const char* username = "user";
//const char* userpass = "pass";
void sqltables()
{
tables_write();
tables_read();
}
void tables_write()
{
// first connect to data base
// "recreate" option delete all your tables !!!!
TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
if (f->IsZombie()) { delete f; return; }
// you can change configuration only until first object
// is written to TSQLFile
f->SetUseSuffixes(kFALSE);
f->SetArrayLimit(1000);
f->SetUseIndexes(1);
// f->SetTablesType("ISAM");
// f->SetUseTransactions(kFALSE);
// lets first write histogram
TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
h1->FillRandom("gaus",10000);
h1->Write("histo");
// here we create list of objects and store them as single key
// without kSingleKey all TBox objects will appear as separate keys
TList* arr = new TList;
for(Int_t n=0;n<10;n++) {
TBox* b = new TBox(n*10,n*100,n*20,n*200);
arr->Add(b, Form("option_%d_option",n));
}
arr->Write("list",TObject::kSingleKey);
// clones array is also stored as single key
TClonesArray clones("TBox",10);
for(int n=0;n<10;n++)
new (clones[n]) TBox(n*10,n*100,n*20,n*200);
clones.Write("clones",TObject::kSingleKey);
// close connection to database
delete f;
}
void tables_read()
{
// now open connection to database for read-only
TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
if (f->IsZombie()) { delete f; return; }
// see list of keys
f->ls();
// get histogram from DB and draw it
TH1* h1 = (TH1*) f->Get("histo");
if (h1!=0) {
h1->Draw();
}
// get TList with other objects
TObject* obj = f->Get("list");
cout << "Printout of TList object" << endl;
if (obj!=0) obj->Print("*");
delete obj;
// and get TClonesArray
obj = f->Get("clones");
cout << "Printout of TClonesArray object" << endl;
if (obj!=0) obj->Print("*");
delete obj;
// this is query to select data of hole class from different tables
cout << "================ TBox QUERY ================ " << endl;
cout << f->MakeSelectQuery(TBox::Class()) << endl;
cout << "================ END of TBox QUERY ================ " << endl;
cout << "================== TH1I QUERY ================ " << endl;
cout << f->MakeSelectQuery(TH1I::Class()) << endl;
cout << "================ END of TH1I QUERY ================ " << endl;
// close connection to database
delete f;
}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2456
Create a Box.
Definition: TBox.h:22
static TClass * Class()
An array of clone (identical) objects.
Definition: TClonesArray.h:29
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Write all objects in this collection.
1-D histogram with an int per channel (see TH1 documentation)}
Definition: TH1.h:533
static TClass * Class()
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition: TH1.cxx:8804
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition: TH1.cxx:3513
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3060
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
Mother of all ROOT objects.
Definition: TObject.h:41
@ kSingleKey
write collection with single key
Definition: TObject.h:91
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:868
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition: TObject.cxx:624
Access an SQL db via the TFile interface.
Definition: TSQLFile.h:30
const Int_t n
Definition: legend1.C:16
TH1F * h1
Definition: legend1.C:5
Author
Sergey Linev

Definition in file sqltables.C.