View Full Version : export file MySql to QtSql

18th December 2010, 01:23
Hi guys, i have file exported from mysql "db.sql" and i would like to improt it to Qtsql "data.db".
i tried to create the db with:

import sys, os
from PyQt4 import QtCore, QtSql
filename = "data.db"
create = not QtCore.QFile.exists(filename)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
query = QtSql.QSqlQuery()
query.exec_("opencoffe.db < opencoffe.sql")

only create the file "data.db" but don't export anything.

18th December 2010, 02:04
I would use a convertor tool from mysql to sqlite. You can google for it.
Then you can open the resulting sqlite file like you did.


19th December 2010, 00:22
from console(sqlite), to import a file, it does with:
sqlite database.db < file.sql is there another way to make it into my program??

20th December 2010, 16:35
There is no such thing as "Qtsql". Qt provides an interfaces to SQL database engines that include MySQL, SQLite, PostgreSQL and several others. So far, the answer have been centered around putting the data into an SQLite database since that is what your code fragment showed. With that in mind, so I will keep on that track...

One way of doing the import would be to brute force the command line from within your code using a "system()" call.

system("sqlite mydatabase.db < some.sql");

Another option would be to open the input file, read it, discard all comments, and then pass the queries to the database using QSqlQuery. [Since I am too lazy to figure out all of the Qt details of reading stuff from a file and then work out the simple parser, I'll skip the example code. :-) ] The advantage of this is that you can quickly determine when something fails to parse -- which brings me to my next point.

The bigger issue you will probably have to worry about is making sure the exported SQL from MySQL is something SQLite can handle. Each database engine tends to speak its own dialect of SQL. 90% of the language is the same (the SELECTs, INSERTs, etc.) but often the data types differ slightly. That's where you might run into some issues with importing.

Personally, if you have MySQL available on the platform you are going to be running the Qt app on, I would use MySQL for the database engine instead of SQLite so that you don't have to worry about the changing the sql. It's easy to do in your app, simply change "QSQLITE" to "QMYSQL".

db = QtSql.QSqlDatabase.addDatabase("QMYSQL")


21st December 2010, 03:19
i didn't think of that i guess that will be enoug to fix my promblem.
thankyou very much.