Logo ROOT   6.07/09
Reference Guide
sqlselect.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_sql
3 ///
4 /// \macro_code
5 ///
6 /// \author Sergey Linev
7 
8 #include <TSQLServer.h>
9 #include <TSQLResult.h>
10 #include <TSQLRow.h>
11 
12 
13 void sqlselect()
14 {
15  TSQLServer *db = TSQLServer::Connect("mysql://localhost/test","nobody", "");
16 
17  printf("Server info: %s\n", db->ServerInfo());
18 
19  TSQLRow *row;
20  TSQLResult *res;
21 
22  // list databases available on server
23  printf("\nList all databases on server %s\n", db->GetHost());
24  res = db->GetDataBases();
25  while ((row = res->Next())) {
26  printf("%s\n", row->GetField(0));
27  delete row;
28  }
29  delete res;
30 
31  // list tables in database "test" (the permission tables)
32  printf("\nList all tables in database \"test\" on server %s\n",
33  db->GetHost());
34  res = db->GetTables("test");
35  while ((row = res->Next())) {
36  printf("%s\n", row->GetField(0));
37  delete row;
38  }
39  delete res;
40 
41  // list columns in table "runcatalog" in database "mysql"
42  printf("\nList all columns in table \"runcatalog\" in database \"test\" on server %s\n",
43  db->GetHost());
44  res = db->GetColumns("test", "runcatalog");
45  while ((row = res->Next())) {
46  printf("%s\n", row->GetField(0));
47  delete row;
48  }
49  delete res;
50 
51  // start timer
53  timer.Start();
54 
55  // query database and print results
56  const char *sql = "select dataset,rawfilepath from test.runcatalog "
57  "WHERE tag&(1<<2) AND (run=490001 OR run=300122)";
58 // const char *sql = "select count(*) from test.runcatalog "
59 // "WHERE tag&(1<<2)";
60 
61  res = db->Query(sql);
62 
63  int nrows = res->GetRowCount();
64  printf("\nGot %d rows in result\n", nrows);
65 
66  int nfields = res->GetFieldCount();
67  for (int i = 0; i < nfields; i++)
68  printf("%40s", res->GetFieldName(i));
69  printf("\n");
70  for (int i = 0; i < nfields*40; i++)
71  printf("=");
72  printf("\n");
73 
74  for (int i = 0; i < nrows; i++) {
75  row = res->Next();
76  for (int j = 0; j < nfields; j++) {
77  printf("%40s", row->GetField(j));
78  }
79  printf("\n");
80  delete row;
81  }
82 
83  delete res;
84  delete db;
85 
86  // stop timer and print results
87  timer.Stop();
88  Double_t rtime = timer.RealTime();
89  Double_t ctime = timer.CpuTime();
90 
91  printf("\nRealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
92 }
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
static TSQLServer * Connect(const char *db, const char *uid, const char *pw)
The db should be of the form: <dbms>://<host>[:<port>][/<database>], e.g.
Definition: TSQLServer.cxx:61
virtual TSQLResult * GetDataBases(const char *wild=0)=0
virtual TSQLResult * Query(const char *sql)=0
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
virtual Int_t GetRowCount() const
Definition: TSQLResult.h:47
TStopwatch timer
Definition: pirndm.C:37
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
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
const char * GetHost() const
Definition: TSQLServer.h:102
Stopwatch class.
Definition: TStopwatch.h:30