ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
sqlselect.C
Go to the documentation of this file.
1 #ifndef __CINT__
2 #include <TSQLServer.h>
3 #include <TSQLResult.h>
4 #include <TSQLRow.h>
5 #endif
6 
7 void sqlselect()
8 {
9  TSQLServer *db = TSQLServer::Connect("mysql://localhost/test","nobody", "");
10 
11  printf("Server info: %s\n", db->ServerInfo());
12 
13  TSQLRow *row;
14  TSQLResult *res;
15 
16  // list databases available on server
17  printf("\nList all databases on server %s\n", db->GetHost());
18  res = db->GetDataBases();
19  while ((row = res->Next())) {
20  printf("%s\n", row->GetField(0));
21  delete row;
22  }
23  delete res;
24 
25  // list tables in database "test" (the permission tables)
26  printf("\nList all tables in database \"test\" on server %s\n",
27  db->GetHost());
28  res = db->GetTables("test");
29  while ((row = res->Next())) {
30  printf("%s\n", row->GetField(0));
31  delete row;
32  }
33  delete res;
34 
35  // list columns in table "runcatalog" in database "mysql"
36  printf("\nList all columns in table \"runcatalog\" in database \"test\" on server %s\n",
37  db->GetHost());
38  res = db->GetColumns("test", "runcatalog");
39  while ((row = res->Next())) {
40  printf("%s\n", row->GetField(0));
41  delete row;
42  }
43  delete res;
44 
45  // start timer
47  timer.Start();
48 
49  // query database and print results
50  const char *sql = "select dataset,rawfilepath from test.runcatalog "
51  "WHERE tag&(1<<2) AND (run=490001 OR run=300122)";
52 // const char *sql = "select count(*) from test.runcatalog "
53 // "WHERE tag&(1<<2)";
54 
55  res = db->Query(sql);
56 
57  int nrows = res->GetRowCount();
58  printf("\nGot %d rows in result\n", nrows);
59 
60  int nfields = res->GetFieldCount();
61  for (int i = 0; i < nfields; i++)
62  printf("%40s", res->GetFieldName(i));
63  printf("\n");
64  for (int i = 0; i < nfields*40; i++)
65  printf("=");
66  printf("\n");
67 
68  for (int i = 0; i < nrows; i++) {
69  row = res->Next();
70  for (int j = 0; j < nfields; j++) {
71  printf("%40s", row->GetField(j));
72  }
73  printf("\n");
74  delete row;
75  }
76 
77  delete res;
78  delete db;
79 
80  // stop timer and print results
81  timer.Stop();
82  Double_t rtime = timer.RealTime();
83  Double_t ctime = timer.CpuTime();
84 
85  printf("\nRealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
86 }
tuple row
Definition: mrt.py:26
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:108
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:56
virtual TSQLResult * GetDataBases(const char *wild=0)=0
virtual TSQLResult * Query(const char *sql)=0
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:123
virtual Int_t GetRowCount() const
Definition: TSQLResult.h:47
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:75
virtual TSQLResult * GetColumns(const char *dbname, const char *table, const char *wild=0)=0
virtual const char * ServerInfo()=0
virtual TSQLResult * GetTables(const char *dbname, const char *wild=0)=0
double Double_t
Definition: RtypesCore.h:55
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
void sqlselect()
Definition: sqlselect.C:7
const char * GetHost() const
Definition: TSQLServer.h:102
Stopwatch class.
Definition: TStopwatch.h:30