PDA

View Full Version : qt read csv write sqlite



chris.kzn
27th May 2015, 09:33
Hi all,

I am trying to load data from a csv file into a QTableView and I want to insert this data into a SQLite database, how does one transfer data that is in a QTableView into the SQLite database. I got the code of loading the csv file into the QTableView from this link: http://qtsimplify.blogspot.com/2013/02/dealing-with-csv-files-easy-way.html .I am only able to find solutions whereby the initial query was instantiated from a database and so updating of certain fields is permitted but this to load all values that are present in the current QTableView into the SQLite database, let us say a table called MyCSV.

Thanking you in advance. It would help providing code with the explanation if possible please.

anda_skoa
27th May 2015, 11:06
You just run QSqlQuery INSERT queries on the database as you iterate over the data row by row.

Cheers,
_

chris.kzn
27th May 2015, 11:34
Hi anda_skoa,

Is that the only and best method suggested by Qt? In visual studio and lazarus, you are able to save the datagridview in one go. I was hoping that Qt would offer the same sort of saving feature. If not, then I guess I will just need to try it using your method.

Thanking you in advance.

anda_skoa
27th May 2015, 12:18
If you back the view with a QSql based model than you can add rows there.

I personally would have done the import first and then the visualization, keeping the data handling independent of any UI, nicely unit testable, etc.

Cheers,
_

chris.kzn
27th May 2015, 12:52
Hi anda_skoa,

The reason I have opted to go wih the viewer before applying the updates as it gives the user a graphical view to make sure the csv file is in the correct format and I also only wish to update a few records from the actual table rather than the whole table. So I need to allow for the user to specify the fields of the table which the uploaded csv file will update. I however have found an example of what you are saying about diirectly importing it into SQLite prior to the user seeing it but if the file is in the incorrect format, the user will not be able to fix it unless they manually do the work through a text editor.

http://dustri.org/b/import-cvs-to-sqlite-with-qt.html

I can only think of applying the QTableView to a QSqlTableModel but how would one go about applying that?

I think I have the write project group - they seem to be on my wave-length - http://sqlitebrowser.org/ - all designed with Qt.