Index: trunk/FACT++/src/rootifysql.cc
===================================================================
--- trunk/FACT++/src/rootifysql.cc	(revision 18925)
+++ trunk/FACT++/src/rootifysql.cc	(revision 18927)
@@ -169,19 +169,9 @@
 
     // -------------------------- Request data from database -------------------
-    const mysqlpp::StoreQueryResult res =
-        Database(uri).query(query).store();
-    // -------------------------------------------------------------------------
-
-    if (verbose>0)
-    {
-        cout << res.size() << " rows received." << endl;
-        cout << "Query time: " << Time().UnixTime()-start2.UnixTime() << "s" << endl;
-    }
-
-    if (res.empty())
-    {
-        cerr << "Nothing to write." << endl;
-        return 5;
-    }
+    Database connection(uri); // Keep alive while fetching rows
+
+    const mysqlpp::UseQueryResult res =
+        connection.query(query).use();
+    // -------------------------------------------------------------------------
 
     if (verbose>0)
@@ -191,16 +181,23 @@
     TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
     if (tfile.IsZombie())
+        return 5;
+
+    // -------------------------------------------------------------------------
+
+    // get the first row to get the field description
+    mysqlpp::Row row = res.fetch_row();
+    if (!row)
+    {
+        cerr << "Empty set returned... nothing to write." << endl;
         return 6;
-    // -------------------------------------------------------------------------
-
-    const mysqlpp::Row &r = res.front();
-
-    if (verbose>0)
-        cout << "Trying to setup " << r.size() << " branches..." << endl;
+    }
+
+    if (verbose>0)
+        cout << "Trying to setup " << row.size() << " branches..." << endl;
 
     if (verbose>1)
         cout << endl;
 
-    const mysqlpp::FieldNames &l = *r.field_list().list;
+    const mysqlpp::FieldNames &l = *row.field_list().list;
 
     vector<double>  buf(l.size());
@@ -213,5 +210,5 @@
     for (size_t i=0; i<l.size(); i++)
     {
-        const string t = r[i].type().sql_name();
+        const string t = row[i].type().sql_name();
 
         if (t.find("DATETIME")!=string::npos)
@@ -258,11 +255,14 @@
 
     // ---------------------- Fill TTree with DB data --------------------------
-    size_t skip = 0;
-    for (auto row=res.begin(); row<res.end(); row++)
-    {
+    size_t count = 0;
+    size_t skip  = 0;
+    do
+    {
+        count++;
+
         ostringstream sout;
 
         size_t idx=0;
-        for (auto col=row->begin(); col!=row->end(); col++, idx++)
+        for (auto col=row.begin(); col!=row.end(); col++, idx++)
         {
             if (display)
@@ -302,5 +302,5 @@
         }
 
-        if (idx==row->size())
+        if (idx==row.size())
         {
             ttree->Fill();
@@ -308,5 +308,9 @@
                 cout << sout.str() << endl;
         }
-    }
+
+        row = res.fetch_row();
+
+    } while (row);
+
     // -------------------------------------------------------------------------
 
@@ -316,4 +320,5 @@
     if (verbose>0)
     {
+        cout << count << " rows fetched." << endl;
         if (skip>0)
             cout << skip << " rows skipped due to NULL field." << endl;
