Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Getting SQLite table schema information



  • I am writing a unit test for my "createTables" method in my db class. I've successfully worked with SQLite databases in the past and am comfortable with the QSqlDatabase (and related) classes. However, I'm not sure how to query the list of columns in a table.

    For example, my createTables method creates a table named "metadata" with key TEXT, value TEXT. My unit test can successfuly check if the table has been created. What I want to do now is check to make sure those two columns exist in the table. I'm not exactly sure how to do that. Do I need to craft a SQL query? Or is there a property on some class that I can query?


  • Lifetime Qt Champion

    Hi,

    If memory serves well, something like:

    SELECT sql 
    FROM sqlite_master 
    WHERE name = 'mytable';
    

    should get you what you want.


  • Lifetime Qt Champion


Log in to reply