How to get the mysql table columns data type?

后端 未结 11 667
独厮守ぢ
独厮守ぢ 2020-12-02 06:23

I want to get the column data type of a mysql table.

Thought I could use MYSQLFIELD structure but it was enumerated field types.

Then I tried wi

相关标签:
11条回答
  • 2020-12-02 06:26

    Please use the below mysql query.

    SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH 
    FROM information_schema.columns 
    WHERE table_schema = '<DATABASE NAME>' 
    AND table_name = '<TABLE NAME>' 
    AND COLUMN_NAME = '<COLOMN NAME>' 
    

    0 讨论(0)
  • 2020-12-02 06:30

    The query below returns a list of information about each field, including the MySQL field type. Here is an example:

    SHOW FIELDS FROM tablename
    /* returns "Field", "Type", "Null", "Key", "Default", "Extras" */
    

    See this manual page.

    0 讨论(0)
  • 2020-12-02 06:36

    You can use the information_schema columns table:

    SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS 
      WHERE table_name = 'tbl_name' AND COLUMN_NAME = 'col_name';
    
    0 讨论(0)
  • 2020-12-02 06:41
    ResultSet rs = Sstatement.executeQuery("SELECT * FROM Table Name");
    
    ResultSetMetaData rsMetaData = rs.getMetaData();
    
    int numberOfColumns = rsMetaData.getColumnCount();
    System.out.println("resultSet MetaData column Count=" + numberOfColumns);
    
    for (int i = 1; i <= numberOfColumns; i++) {
     System.out.println("column number " + i);
    
      System.out.println(rsMetaData.getColumnTypeName(i));
    }
    
    0 讨论(0)
  • 2020-12-02 06:42

    First select the Database using use testDB; then execute

    desc `testDB`.`images`;
    -- or
    SHOW FIELDS FROM images;
    

    Output:

    Get Table Columns with DataTypes

    0 讨论(0)
  • 2020-12-02 06:42

    SHOW COLUMNS FROM mytable

    Self contained complete examples are often useful.

    <?php
      // The server where your database is hosted                 localhost
      // The name of your database                                mydatabase
      // The user name of the database user                       databaseuser
      // The password of the database user                        thesecretpassword
      // Most web pages are in utf-8 so should be the database    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
      try
      {
        $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "databaseuser", "thesecretpassword", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));  
      }
        catch(PDOException $e)
      {
        die('Could not connect: ' . $e->getMessage());
      }
    
      $sql   = "SHOW COLUMNS FROM mytable";
      $query = $pdo->prepare($sql);
      $query->execute();
    
      $err = $query->errorInfo();
      $bug = $err[2];
    
      if ($bug != "") { echo "<p>$bug</p>"; }
    
      while ($row = $query->fetch(PDO::FETCH_ASSOC))
      {
        echo "<pre>" . print_r($row, true) . "</pre>";
      }
    
      /* OUTPUT SAMPLE  
      Array
      (
          [Field] => page_id
          [Type] => char(40)
          [Null] => NO
          [Key] => 
          [Default] => 
          [Extra] => 
      )
    
      Array
      (  
          [Field] => last_name
          [Type] => char(50)
          More ...
      */
    ?>
    
    0 讨论(0)
提交回复
热议问题