Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, March 2008
QSqlQueryModel to CSV?


Message 1 in thread

I would like to be able to take the contents of a QSqlQueryModel or  
QSqlTableModel and dump them to an external file such as a CSV (basic  
"export" functionality). Is there an easy way to do this other than  
just looping through the model and getting the data from each cell?  
Thanks.

-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------


--
 [ signature omitted ] 

Message 2 in thread

On Mar 3, 2008, at 12:31 PM, Israel Brewster wrote:

> I would like to be able to take the contents of a QSqlQueryModel or  
> QSqlTableModel and dump them to an external file such as a CSV  
> (basic "export" functionality). Is there an easy way to do this  
> other than just looping through the model and getting the data from  
> each cell? Thanks.

Actually, a tab-delimited file would be fine here as well, I just need  
to get something that I can toss at excel or the like for portability  
purposes, for when there is no network connection available. Any  
ideas? I'm not seeing anything in the documentation so far. Thanks.

-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
>
>
> -----------------------------------------------
> Israel Brewster
> Computer Support Technician
> Frontier Flying Service Inc.
> 5245 Airport Industrial Rd
> Fairbanks, AK 99709
> (907) 450-7250 x293
> -----------------------------------------------
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 3 in thread

On March 4, 2008 01:42:14 pm Israel Brewster wrote:
> On Mar 3, 2008, at 12:31 PM, Israel Brewster wrote:
> > I would like to be able to take the contents of a QSqlQueryModel or
> > QSqlTableModel and dump them to an external file such as a CSV
> > (basic "export" functionality). Is there an easy way to do this
> > other than just looping through the model and getting the data from
> > each cell? Thanks.
>
> Actually, a tab-delimited file would be fine here as well, I just need
> to get something that I can toss at excel or the like for portability
> purposes, for when there is no network connection available. Any
> ideas? I'm not seeing anything in the documentation so far. Thanks.

It actually shouldn't be too hard for you to just code up something custom to 
get the job done.

Something VAGUELY along the lines of:

for (int row = 0; row < queryModel.rowCount(); ++row) {
  QSqlRecord record = queryModel.record(row);
  for (int field = 0; field < record.count(); ++field) {
    if (field > 0) output << "\t";
    output << record.field(field).value().toString();
  }
  output << "\n";
}

You'll probably have to do some special-case handling of nulls and you will 
want to escape the line that does "output << 
record.field(field).value().toString();" so that it escapes the tab character 
and probably the newline character appropriately.  I'll warn you that MS 
Excel is rather picky and seems to have its own interpretation of CSV and TSV 
files that are different from the rest of the world.  They like to wrap 
things in quotes, for example, and do weird things with numbers.  Still, this 
may be a sufficient start for you.

--
 [ signature omitted ] 

Message 4 in thread



On Mar 4, 2008, at 11:51 AM, Chris Thompson wrote:

> On March 4, 2008 01:42:14 pm Israel Brewster wrote:
>> On Mar 3, 2008, at 12:31 PM, Israel Brewster wrote:
>>> I would like to be able to take the contents of a QSqlQueryModel or
>>> QSqlTableModel and dump them to an external file such as a CSV
>>> (basic "export" functionality). Is there an easy way to do this
>>> other than just looping through the model and getting the data from
>>> each cell? Thanks.
>>
>> Actually, a tab-delimited file would be fine here as well, I just  
>> need
>> to get something that I can toss at excel or the like for portability
>> purposes, for when there is no network connection available. Any
>> ideas? I'm not seeing anything in the documentation so far. Thanks.
>
> It actually shouldn't be too hard for you to just code up something  
> custom to
> get the job done.
>
> Something VAGUELY along the lines of:
>
> for (int row = 0; row < queryModel.rowCount(); ++row) {
>  QSqlRecord record = queryModel.record(row);
>  for (int field = 0; field < record.count(); ++field) {
>    if (field > 0) output << "\t";
>    output << record.field(field).value().toString();
>  }
>  output << "\n";
> }
Thanks. Yeah, I was thinking of something like that, but I was hoping  
for a more elegant/efficient solution, along the lines of a  
queryModel.export(stream *) or something. Course, there may not be  
one, but it would have been nice :)

-----------------------------------------------
Israel Brewster
Computer Support Technician
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------
>
>
> You'll probably have to do some special-case handling of nulls and  
> you will
> want to escape the line that does "output <<
> record.field(field).value().toString();" so that it escapes the tab  
> character
> and probably the newline character appropriately.  I'll warn you  
> that MS
> Excel is rather picky and seems to have its own interpretation of  
> CSV and TSV
> files that are different from the rest of the world.  They like to  
> wrap
> things in quotes, for example, and do weird things with numbers.   
> Still, this
> may be a sufficient start for you.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ]