为了提高易用性,我希望我的mysql连接包装类能够自动确定命令属性的类型。我假设我需要的信息可以通过连接器api获得,但我不知道如何获取列的名称和类型的命令?
答案 0 :(得分:1)
表名:
SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'your database name'
或者
SHOW TABLES IN db_name
对于列:
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
根据您的要求选择字段。
答案 1 :(得分:1)
许多方式,许多专栏,挑选和选择:
SELECT column_name, data_type,is_nullable,character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'course' and TABLE_SCHEMA='so_gibberish';
+-------------+-----------+-------------+--------------------------+
| column_name | data_type | is_nullable | character_maximum_length |
+-------------+-----------+-------------+--------------------------+
| course_id | varchar | NO | 8 |
| title | varchar | YES | 50 |
| dept_name | varchar | YES | 20 |
| credits | decimal | YES | NULL |
+-------------+-----------+-------------+--------------------------+