ROOT logo
#ifndef __CINT__
#include <TSQLServer.h>
#include <TSQLResult.h>
#include <TSQLRow.h>
#endif
   
void sqlselect()
{
   TSQLServer *db = TSQLServer::Connect("mysql://localhost/test","nobody", "");

   printf("Server info: %s\n", db->ServerInfo());
   
   TSQLRow *row;
   TSQLResult *res;
   
   // list databases available on server
   printf("\nList all databases on server %s\n", db->GetHost());
   res = db->GetDataBases();
   while ((row = res->Next())) {
      printf("%s\n", row->GetField(0));
      delete row;
   }
   delete res;

   // list tables in database "test" (the permission tables)
   printf("\nList all tables in database \"test\" on server %s\n",
          db->GetHost());
   res = db->GetTables("test");
   while ((row = res->Next())) {
      printf("%s\n", row->GetField(0));
      delete row;
   }
   delete res;
   
   // list columns in table "runcatalog" in database "mysql"
   printf("\nList all columns in table \"runcatalog\" in database \"test\" on server %s\n",
          db->GetHost());
   res = db->GetColumns("test", "runcatalog");
   while ((row = res->Next())) {
      printf("%s\n", row->GetField(0));
      delete row;
   }
   delete res;

   // start timer
   TStopwatch timer;
   timer.Start();

   // query database and print results
   const char *sql = "select dataset,rawfilepath from test.runcatalog "
                     "WHERE tag&(1<<2) AND (run=490001 OR run=300122)";
//   const char *sql = "select count(*) from test.runcatalog "
//                     "WHERE tag&(1<<2)";
   
   res = db->Query(sql);

   int nrows = res->GetRowCount();
   printf("\nGot %d rows in result\n", nrows);
   
   int nfields = res->GetFieldCount();
   for (int i = 0; i < nfields; i++)
      printf("%40s", res->GetFieldName(i));
   printf("\n");
   for (int i = 0; i < nfields*40; i++)
      printf("=");
   printf("\n");
   
   for (int i = 0; i < nrows; i++) {
      row = res->Next();
      for (int j = 0; j < nfields; j++) {
         printf("%40s", row->GetField(j));
      }
      printf("\n");
      delete row;
   }
   
   delete res;
   delete db;

   // stop timer and print results
   timer.Stop();
   Double_t rtime = timer.RealTime();
   Double_t ctime = timer.CpuTime();

   printf("\nRealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime);
}
 sqlselect.C:1
 sqlselect.C:2
 sqlselect.C:3
 sqlselect.C:4
 sqlselect.C:5
 sqlselect.C:6
 sqlselect.C:7
 sqlselect.C:8
 sqlselect.C:9
 sqlselect.C:10
 sqlselect.C:11
 sqlselect.C:12
 sqlselect.C:13
 sqlselect.C:14
 sqlselect.C:15
 sqlselect.C:16
 sqlselect.C:17
 sqlselect.C:18
 sqlselect.C:19
 sqlselect.C:20
 sqlselect.C:21
 sqlselect.C:22
 sqlselect.C:23
 sqlselect.C:24
 sqlselect.C:25
 sqlselect.C:26
 sqlselect.C:27
 sqlselect.C:28
 sqlselect.C:29
 sqlselect.C:30
 sqlselect.C:31
 sqlselect.C:32
 sqlselect.C:33
 sqlselect.C:34
 sqlselect.C:35
 sqlselect.C:36
 sqlselect.C:37
 sqlselect.C:38
 sqlselect.C:39
 sqlselect.C:40
 sqlselect.C:41
 sqlselect.C:42
 sqlselect.C:43
 sqlselect.C:44
 sqlselect.C:45
 sqlselect.C:46
 sqlselect.C:47
 sqlselect.C:48
 sqlselect.C:49
 sqlselect.C:50
 sqlselect.C:51
 sqlselect.C:52
 sqlselect.C:53
 sqlselect.C:54
 sqlselect.C:55
 sqlselect.C:56
 sqlselect.C:57
 sqlselect.C:58
 sqlselect.C:59
 sqlselect.C:60
 sqlselect.C:61
 sqlselect.C:62
 sqlselect.C:63
 sqlselect.C:64
 sqlselect.C:65
 sqlselect.C:66
 sqlselect.C:67
 sqlselect.C:68
 sqlselect.C:69
 sqlselect.C:70
 sqlselect.C:71
 sqlselect.C:72
 sqlselect.C:73
 sqlselect.C:74
 sqlselect.C:75
 sqlselect.C:76
 sqlselect.C:77
 sqlselect.C:78
 sqlselect.C:79
 sqlselect.C:80
 sqlselect.C:81
 sqlselect.C:82
 sqlselect.C:83
 sqlselect.C:84
 sqlselect.C:85
 sqlselect.C:86
 sqlselect.C:87