Logo ROOT  
Reference Guide
No Matches
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()
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->SetTablesType("ISAM");
// f->SetUseTransactions(kFALSE);
// lets first write histogram
TH1I* h1 = new TH1I("histo1","histo title", 1000, -4., 4.);
// 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));
// 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);
// 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
// get histogram from DB and draw it
TH1* h1 = (TH1*) f->Get("histo");
if (h1!=0) {
// 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 b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
char * Form(const char *fmt,...)
Create a Box.
Definition TBox.h:22
An array of clone (identical) objects.
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write all objects in this collection.
1-D histogram with an int per channel (see TH1 documentation)}
Definition TH1.h:534
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
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:8777
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3525
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
A doubly linked list.
Definition TList.h:44
virtual void Add(TObject *obj)
Definition TList.h:87
Mother of all ROOT objects.
Definition TObject.h:37
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:798
@ kSingleKey
write collection with single key
Definition TObject.h:87
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:552
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
Sergey Linev

Definition in file sqltables.C.