Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify the structure of a database? (SQLite in C++ / Qt)

I was wondering what the "best" way to verify the structure of my database is with SQLite in Qt / C++. I'm using SQLite so there is a file which contains my database, and I want to make sure that, when launching the program, the database is structured the way it should be- i.e., it has X tables each with their own Y columns, appropriately named, etc. Could someone point my in the right direction? Thanks so much!

like image 297
Joseph Avatar asked Jul 20 '10 04:07

Joseph


People also ask

How can I see the structure of a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

Is SQLite structured?

Another NAND/SD card-based storage that developers leverage is a specific type of file—an SQLite database. Databases are used for structured data storage and SQLite is a popular database format appearing in many mobile systems as well as traditional operating systems.

What is SQLite in Qt?

SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. Used in Qt SQL Lite plugin. Configure Qt with -system-sqlite or -no-sqlite to avoid. The sources can be found in qt5/qtbase/src/3rdparty/sqlite.

Is SQLite structured or unstructured?

Structured data tools SQLite: Implements a self-contained, serverless, zero-configuration, transactional relational database engine.


2 Answers

You can get a list of all the tables in the database with this query:

select tbl_name from sqlite_master;

And then for each table returned, run this query to get column information

pragma table_info(my_table);

For the pragma, each row of the result set will contain: a column index, the column name, the column's type affinity, whether the column may be NULL, and the column's default value.

(I'm assuming here that you know how to run SQL queries against your database in the SQLite C interface.)

like image 197
pkh Avatar answered Oct 27 '22 08:10

pkh


If you have QT and thus QtSql at hand, you can also use the QSqlDatabase::tables() (API doc) method to get the tables and QSqlDatabase::record(tablename) to get the field names. It can also give you the primary key(s), but for further details you will have to follow pkh's advice to use the table_info pragma.

like image 35
quazgar Avatar answered Oct 27 '22 08:10

quazgar