In MySQL, I know I can list the tables in a database with:
JavaScript
x
2
1
SHOW TABLES
2
But I want to insert a table name into a specified table, for example:
JavaScript
1
4
1
INSERT INTO dataset_names (dataset)
2
SELECT table_name FROM information_schema.tables
3
WHERE table_schema = '%s';
4
But when I execute the above statement, the dataset_names table does not get updated. I created the master table using:
JavaScript
1
2
1
CREATE TABLE dataset_names (id INT AUTO_INCREMENT PRIMARY KEY, dataset text);
2
Here is the python code.
JavaScript
1
13
13
1
dataset_name_query = """
2
INSERT INTO dataset_names (dataset) values(
3
SELECT table_name FROM information_schema.tables
4
WHERE table_schema = '%s');
5
"""% (tablename)
6
7
csv_obj.read()
8
csv_obj.create()
9
cursor = createConnection()
10
#Cursor executing the dataset insert into master table query
11
cursor.execute(dataset_name_query)
12
closeCursor(cursor)
13
Advertisement
Answer
JavaScript
1
2
1
INSERT INTO table_names(name) (SELECT table_name FROM information_schema.tables WHERE table_schema='database_name')
2
Here the order on insert values and select values must match;