Now I want to get table name from create table sql, such as
CREATE TABLE `engineers` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`email` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
`birth` datetime,
PRIMARY KEY (`id`),
INDEX(`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
or
CREATE TABLE IF NOT EXISTS .....
If you can query information_schema
of your database server then you access your table name from the result of following query.
select table_name,
create_time AS `Time`
from information_schema.tables
where table_schema = @dbName
order by create_time desc
limit 1
This should be run right after table creation is done
table_name Time
table_20200815 15.08.2020 12:06:25
This is in php, but should be easy to convert to ruby. Assuming the Create statement is in $sql...
[EDITED to allow for "(" to follow tablename, and fix parameter order in strpos()]
$tablename = trim(substring($sql,strpos($sql,"TABLE IF NOT EXISTS") === 0 ? strpos($sql,"TABLE") + 5 : strpos($sql,"TABLE IF NOT EXISTS") + 20));
$tablename = substring($tablename,0,strpos($tablename,strpos($tablename," ") < strpos($tablename,"(") ? " " : "("));
Then test for and strip backticks if there are any.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.