Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get Table schema information ?

Newbie
 
Join Date: Jun 2009
Posts: 19
#1: Jun 12 '09
How can we get the table schema information in MYSQL 5.1.34?

Like we use in ORACLE to get it as:

OCIDescribeAny() // //get the describe handle for the table to be described.

OCIAttrGet() // to get parameter handle

OCIAttrGet() ////get the number of columns in the table

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Jun 12 '09

re: How to get Table schema information ?


Hi.

Well, there is always the INFORMATION_SCHEMA database.
You can search for every detail about your databases there.

Then there is the SHOW command and various other Utility Statements.

Is that what you are looking for?
Newbie
 
Join Date: Jun 2009
Posts: 19
#3: Jun 15 '09

re: How to get Table schema information ?


Thanks. I got the information from INFORMATION_SCHEMA COLUMNS.

While retrieving the data from the result of query I faced following problem.
Expand|Select|Wrap|Line Numbers
  1. mysql> select select COLUMN_NAME,  DATA_TYPE, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION from INFORMATION_SCHEMA.COLUMNS where table_name='account');
In coding I run the query using mysql_real_query() whose result is stored in
Expand|Select|Wrap|Line Numbers
  1. pResult = mysql_store_result(pMySQLDB);
  2. fields = mysql_fetch_field(pResult);
  3. my_ulonglong noOfRows =  mysql_num_rows(pResult);
  4. row = mysql_fetch_row(pResult);
it will not return any rows.

But in fields I get select COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION that I don't want.
I want the value like "amit, "MY_VAR_STRING", '10', '0'.

I want to store these data into another variable however not getting it.


Can you please tell me how do I fetch the data what I want from the query output or I am doing anything wrong?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#4: Jun 15 '09

re: How to get Table schema information ?


I think you are confusing the old MySQL (mysql) extension with the new Improved MySQL (mysqli) extension.

The function mysqli_real_query belongs to the latter.

Note the "i" following the "mysql" in the function name.
All the functions belonging to the Improved MySQL extension also have the trailing "i" in the function name.

This is how these function should be used:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Connect to MySQL using the procedural style mysqli functions.
  3. $dbLink = mysqli_connect("localhost", "user", "passwd", "dbName");
  4. if(mysqli_connect_errno())
  5. {
  6.     printf("MySQL Connection failed! %s", mysqli_connect_error());
  7. }
  8.  
  9. // Create a query to execute.
  10. $sql = "SELECT
  11.             COLUMN_NAME,
  12.             DATA_TYPE, IS_NULLABLE,
  13.             CHARACTER_MAXIMUM_LENGTH,
  14.             NUMERIC_PRECISION
  15.         FROM INFORMATION_SCHEMA.COLUMNS
  16.         WHERE
  17.             table_name='tableName'
  18.         AND table_schema='dbName'";
  19.  
  20. // Execute the query
  21. if(mysqli_real_query($dbLink, $sql))
  22. {
  23.     // Fetch the results
  24.     $result = mysqli_store_result($dbLink);
  25.  
  26.     // Print the number of rows returned
  27.     printf("Query succeeded! (%d rows returned)<br />", mysqli_num_rows($result));
  28.  
  29.     // Print the results
  30.     $rowNo = 0;
  31.     while($row = mysqli_fetch_assoc($result)) {
  32.         echo '<br />Row #', (++$rowNo) ,'<br />';
  33.         foreach($row as $_colName => $_colValue) {
  34.             echo " - {$_colName} = {$_colValue}<br />";
  35.         }
  36.     }
  37.  
  38.     // Free the result object
  39.     mysqli_free_result($result);
  40. }
  41. else {
  42.     printf("Query execution failed! %s", mysqli_error($dbLink));
  43. }
  44.  
  45. // Close the connection
  46. mysqli_close($dbLink);
  47. ?>
Note that I added the 'table_schema' column to the query. I used it to filter the query based on the database name, as well as the table name.
If you don't want that, just remove it.
Newbie
 
Join Date: Jun 2009
Posts: 19
#5: Jun 15 '09

re: How to get Table schema information ?


I am using API syntax from MySQL 5.1reference mannual which is recommended version.
which does not use 'i' as 'mysqli'.

I am writing application in C++.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#6: Jun 15 '09

re: How to get Table schema information ?


Quote:

Originally Posted by amit2781 View Post

I am writing application in C++.

That's the sort of thing we need to know straight away, or we will have to waste time guessing.

Going through the C API in the manual, I'm guessing this is how you use that function:
(Note, I don't really know C/C++, so the syntax may be a bit off)
Expand|Select|Wrap|Line Numbers
  1. // Execute query
  2. result_success = mysql_real_query(&connection, &query_string, query_length);
  3. if(!result_success) {
  4.     // Error. Query failed
  5. }
  6. else {
  7.     // Get the result
  8.     result_resource = mysql_store_result(&connection);
  9.     if(!result_resource) {
  10.         // Error. Failed to fetch resource.
  11.     }
  12.     else {
  13.         // Get field definitions
  14.         num_fields = mysql_num_fields(result_resource);
  15.         fields = mysql_fetch_fields(result_resource)
  16.  
  17.         // Get row count
  18.         row_count = mysql_num_rows(result_resource);
  19.  
  20.         // Fetch rows
  21.         while((row = mysql_fetch_row(result_resource))) {
  22.             // Do whatever needs to happen to the rows...
  23.         }
  24.  
  25.         // Free the result
  26.         mysql_free_result(result_resource);
  27.     }
  28. }
Newbie
 
Join Date: Jun 2009
Posts: 19
#7: Jun 16 '09

re: How to get Table schema information ?


Thanks you very much Atli.

I solved my problem.
Newbie
 
Join Date: Jun 2009
Posts: 19
#8: Jun 17 '09

re: How to get Table schema information ?


How do I get the native data type like we get in ORACLE using

OCIAttrGet(colHandle, OCI_DTYPE_PARAM, (dvoid *) &dataType, 0,
OCI_ATTR_DATA_TYPE, m_errorHandle);

It will return &datatype value for:
VARCHAR2 - 1
NUMBER - 2
Date - 12

How do we achieve this in MYSQL 5.1?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#9: Jun 17 '09

re: How to get Table schema information ?


Perhaps this is what your are looking for?
21.10.3.19. mysql_fetch_fields()
Newbie
 
Join Date: Jun 2009
Posts: 19
#10: Jun 17 '09

re: How to get Table schema information ?


I think it is right that What I require will get it from -
enum enum_field_types Ex.
for TINY - 0, SMALLINT - 1 and so on.

As this is enum types so will consider the 0, 1, 2 ... so on.
But need to see in more details.

Thanks.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#11: Jun 17 '09

re: How to get Table schema information ?


How about:
Expand|Select|Wrap|Line Numbers
  1. MYSQL_FIELD *field;
  2. while((field = mysql_fetch_field(result)))
  3. {
  4.     printf("field name %s\n", field->type);
  5. }
Wouldn't that print the type of the field?
The MYSQL_FIELD::type is a enum_field_types enum.
Newbie
 
Join Date: Jun 2009
Posts: 19
#12: Jun 17 '09

re: How to get Table schema information ?


Yes it will give for the fields and not for the rows.
like
mysql> select column_name, column_type from information_schema.columns where table_name='account' and table_schema='amit';
---------------------------------------------
| column_name | column_type |
---------------------------------------------|
| acct_num | int(11) |
| amount | decimal(2,0) |
---------------------------------------------

It will give me
MYSQL_TYPE_VAR_STRING for column_name where I required like
MYSQL_TYPE_LONG for "acct_num" .

If we will see in ORACLE it is giving the values for "acct_num" as 2 which indicates INT data type native value.
Newbie
 
Join Date: Jun 2009
Posts: 19
#13: Jul 2 '09

re: How to get Table schema information ?


Hi,

I have created table -== account(acct_num INT, amount INT);
I have created view using

CREATE VIEW v AS SELECT acct_num AS value FROM account;

when I execute following :
mysql> select table_name from information_schema.views;
mysql> select table_name from information_schema.tables;
+------------+
| table_name |
+------------+
| v |
+------------+

If 'v' is only the view then why it is present in information_schema.tables instead only in information_schema.views?

My problem is when I fire query to get table_name it will return me 'v' also from information_schema.tables which I don't want.
Newbie
 
Join Date: Jun 2009
Posts: 19
#14: Jul 10 '09

re: How to get Table schema information ?


I have uninstalled MySQL 5.1.34 and tried to reinstalled but it is giving me error as :
The security settings could not be applied to the database because the connection has failed with the following error.

Error Nr. 1045
Access denied for user 'root'@'localhost' (using password: YES)

If a personal firewall is running on your machine, please make sure you have opened the TCP port 3306 for connections. Otherwise no client application can connect to the server. After you have opened the port please press [Retry] to apply the security settings.

If you are re-installing after you just uninstalled the MySQL server please note that the data directory was not removed automatically. Therefore the old password from your last installation is still needed to connect to the server. In this case please select skip now and re-run the Configuration Wizard from the start menu.

I tried from start menu also but the same thing happens.

Is there anything wrong while installation?
Reply