Results 1 to 9 of 9

Thread: Check if a table exists in a mysql database

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Check if a table exists in a mysql database

    Hi,
    After connectig to a database i whant to check if a database is already created and with a specific structure.
    As an example:mysql> describe Teste;
    Qt Code:
    1. +---------+---------+------+-----+---------+-------+
    2. | Field | Type | Null | Key | Default | Extra |
    3. +---------+---------+------+-----+---------+-------+
    4. | field01 | int(11) | NO | PRI | NULL | |
    5. | field02 | int(11) | YES | | NULL | |
    6. +---------+---------+------+-----+---------+-------+
    To copy to clipboard, switch view to plain text mode 

    What sort of query showld i use?

    Thanks

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    read this.
    you should use CREATE TABLE IF NOT EXISTS.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    I know about create table if not exists ... but that's not the point.
    It ould create the table, when i just whant to know if it exists, and with a specific structure

  4. #4
    Join Date
    May 2009
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    Query that table with

    SELECT field1, field2, (for all fields) FROM Teste

    If you get an error, you know it doesn't exist or a field within the table doesn't exist (checks structure and existence). As far as testing each field's attributes, I don't know off hand how to go that far with it.

  5. The following user says thank you to MikeG for this useful post:

    graciano (3rd November 2009)

  6. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    RTFM look at SHOW statements in MySQL manual

  7. #6
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    Well ... if we take the RTFM strictly then we could close the forum.
    It is all in the Manual ... just take the time to read all of them
    Any way ... thanks for nothing.

  8. #7
    Join Date
    Nov 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Check if a table exists in a mysql database

    Maybe he's trying to point you at "show create table <tablename>"?

    You can do:

    show tables like '<tablename>'

    first to get an idea whether the table exists.

    TBH I've never used these in code, only at the mysql prompt, so I don't know how you would go about parsing the table structure from the output of show create table, but maybe it will get you going..

  9. #8
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    I think that the best approach for what i pretend will be looking directly into the information_schema database and then just look into the right table.
    Something like this:
    Qt Code:
    1. mysql> select COLUMN_NAME, DATA_TYPE from COLUMNS where TABLE_NAME = "T1";
    2. +-------------+-----------+
    3. | COLUMN_NAME | DATA_TYPE |
    4. +-------------+-----------+
    5. | a | int |
    6. | b | int |
    7. | x | int |
    8. +-------------+-----------+
    To copy to clipboard, switch view to plain text mode 
    This will require special privileges for the user but ... if i just grant the right privileges there will be no problem.

  10. #9
    Join Date
    May 2008
    Location
    New Zealand
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Check if a table exists in a mysql database

    If you want to use the builtin stuff in QT, you can use QSqlDatabase::tables() once you have opened the database and check if the table is in the QStringList returned.

    Then you can use QSqlRecord QSqlDatabase::record ( const QString & tablename ) to get the table structure.

  11. The following user says thank you to Netwiz for this useful post:

    graciano (5th November 2009)

Similar Threads

  1. problem creating a mysql database
    By TonyB in forum Qt Programming
    Replies: 10
    Last Post: 23rd July 2010, 15:39
  2. Replies: 1
    Last Post: 16th July 2009, 02:25
  3. Accessing xampp mysql database
    By synack in forum Newbie
    Replies: 8
    Last Post: 19th March 2009, 09:08
  4. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48
  5. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50

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.