Results 1 to 5 of 5

Thread: export file MySql to QtSql

  1. #1
    Join Date
    May 2010
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Question export file MySql to QtSql

    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:
    Qt Code:
    1. import sys, os
    2. from PyQt4 import QtCore, QtSql
    3. filename = "data.db"
    4. create = not QtCore.QFile.exists(filename)
    5. db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
    6. db.setDatabaseName(filename)
    7. db.open()
    8. query = QtSql.QSqlQuery()
    9. query.exec_("opencoffe.db < opencoffe.sql")
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    99
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: expor file MySql to QtSql

    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.

    Everall

  3. #3
    Join Date
    May 2010
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: expor file MySql to QtSql

    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??

  4. #4
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: export file MySql to QtSql

    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.
    Qt Code:
    1. system("sqlite mydatabase.db < some.sql");
    To copy to clipboard, switch view to plain text mode 

    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".

    Qt Code:
    1. db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
    To copy to clipboard, switch view to plain text mode 

    -John

  5. #5
    Join Date
    May 2010
    Posts
    6
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: export file MySql to QtSql

    i didn't think of that i guess that will be enoug to fix my promblem.
    thankyou very much.

Similar Threads

  1. Replies: 16
    Last Post: 28th April 2020, 14:40
  2. how to export data to a .CSV file
    By qtprgrmr in forum Qt Programming
    Replies: 1
    Last Post: 26th May 2010, 16:22
  3. QT Sqlite database import/ export csv file
    By logesh in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2010, 05:35
  4. QtSql , how to list all databases on the Mysql-Server
    By luoihoc in forum Qt Programming
    Replies: 1
    Last Post: 7th July 2009, 21:52
  5. Help:Export QGraphicsView to Image File
    By cometyang in forum Qt Programming
    Replies: 5
    Last Post: 26th December 2007, 23:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.