Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qsql - How to access table column information ?
Forum Updated to NodeBB v4.3 + New Features

Qsql - How to access table column information ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 399 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jule
    wrote on 1 Nov 2023, 16:05 last edited by
    #1

    Hello, I am trying to access the information of a table columns such as name, type, if it is a primary or foreign key...

    I tried to use QsqlRecord but I cannot get all the information, notably about the primary key. I only have:

    row=selected_index.row()
    column=selected_index.column()
    local_record=self.model_columns.record(row)
     for n in range(0,len(local_record)):
          print("name: "+str(local_record.field(n).name()))
          print("required status: "+str(local_record.field(n).requiredStatus()))
          print("length: "+str(local_record.field(n).length()))
          print("type: "+str(QVariant.typeToName(local_record.field(n).type())))
          print("--------")
    

    Is there a better solution to get a result looking like, for example: my column: name, integer, primary/foreign key, not null ?

    J 1 Reply Last reply 1 Nov 2023, 16:12
    0
    • J Jule
      1 Nov 2023, 16:15

      @JonB Thank you for this information, I will look into the SQLite documentation. And sorry for deleting/moving my old post.

      J Offline
      J Offline
      JonB
      wrote on 1 Nov 2023, 16:21 last edited by
      #4

      @Jule
      I think for SQLite How to read metadata from Sqlite database

      SELECT * FROM sqlite_master WHERE tbl_name = 't1';
      
      J 1 Reply Last reply 1 Nov 2023, 16:33
      1
      • J Jule
        1 Nov 2023, 16:05

        Hello, I am trying to access the information of a table columns such as name, type, if it is a primary or foreign key...

        I tried to use QsqlRecord but I cannot get all the information, notably about the primary key. I only have:

        row=selected_index.row()
        column=selected_index.column()
        local_record=self.model_columns.record(row)
         for n in range(0,len(local_record)):
              print("name: "+str(local_record.field(n).name()))
              print("required status: "+str(local_record.field(n).requiredStatus()))
              print("length: "+str(local_record.field(n).length()))
              print("type: "+str(QVariant.typeToName(local_record.field(n).type())))
              print("--------")
        

        Is there a better solution to get a result looking like, for example: my column: name, integer, primary/foreign key, not null ?

        J Offline
        J Offline
        JonB
        wrote on 1 Nov 2023, 16:12 last edited by JonB 11 Jan 2023, 16:12
        #2

        @Jule
        [I JUST TYPED THE FOLLOWING INTO YOUR QUESTION BUT WHEN I WENT TO SUBMIT I FOUND YOU HAD DELETED YOUR (OLD) TOPIC. PLEASE TRY NOT TO DO THAT! Here it is again.]

        The format of the information is obviously not important --- you can control that once you have the information.

        I don't think Qt SQL code gives you any way to access "meta information" like you want. For example, I don't think it ever looks to see/cares whether a column is in a foreign key.

        The information would not be supplied/obtained in a "standard" way across SQLs (SQLite, MYSQL, MSSQL, ...). But I think you will find each of these offers some way to issue a SQL statement which returns such "meta" information. For example, it may have some system tables with special names on which you can perform a SELECT ... WHERE ... and the columns returned tell you about the tables/columns meta information in the database. You would discover that by consulting the documentation for whichever flavor of SQL you are using.

        J 1 Reply Last reply 1 Nov 2023, 16:15
        0
        • J JonB
          1 Nov 2023, 16:12

          @Jule
          [I JUST TYPED THE FOLLOWING INTO YOUR QUESTION BUT WHEN I WENT TO SUBMIT I FOUND YOU HAD DELETED YOUR (OLD) TOPIC. PLEASE TRY NOT TO DO THAT! Here it is again.]

          The format of the information is obviously not important --- you can control that once you have the information.

          I don't think Qt SQL code gives you any way to access "meta information" like you want. For example, I don't think it ever looks to see/cares whether a column is in a foreign key.

          The information would not be supplied/obtained in a "standard" way across SQLs (SQLite, MYSQL, MSSQL, ...). But I think you will find each of these offers some way to issue a SQL statement which returns such "meta" information. For example, it may have some system tables with special names on which you can perform a SELECT ... WHERE ... and the columns returned tell you about the tables/columns meta information in the database. You would discover that by consulting the documentation for whichever flavor of SQL you are using.

          J Offline
          J Offline
          Jule
          wrote on 1 Nov 2023, 16:15 last edited by Jule 11 Jan 2023, 16:20
          #3

          @JonB Thank you for this information, I will look into the SQLite documentation. And sorry for deleting/moving my old post.

          J 1 Reply Last reply 1 Nov 2023, 16:21
          0
          • J Jule
            1 Nov 2023, 16:15

            @JonB Thank you for this information, I will look into the SQLite documentation. And sorry for deleting/moving my old post.

            J Offline
            J Offline
            JonB
            wrote on 1 Nov 2023, 16:21 last edited by
            #4

            @Jule
            I think for SQLite How to read metadata from Sqlite database

            SELECT * FROM sqlite_master WHERE tbl_name = 't1';
            
            J 1 Reply Last reply 1 Nov 2023, 16:33
            1
            • J JonB
              1 Nov 2023, 16:21

              @Jule
              I think for SQLite How to read metadata from Sqlite database

              SELECT * FROM sqlite_master WHERE tbl_name = 't1';
              
              J Offline
              J Offline
              Jule
              wrote on 1 Nov 2023, 16:33 last edited by
              #5

              @JonB This is exactly what I need, thank you!

              1 Reply Last reply
              0
              • J Jule has marked this topic as solved on 1 Nov 2023, 16:33

              1/5

              1 Nov 2023, 16:05

              • Login

              • Login or register to search.
              1 out of 5
              • First post
                1/5
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved