MySQL批量生成多个表以及批量导入txt文本 - Go语言中文社区

MySQL批量生成多个表以及批量导入txt文本


以下代码是批量生成t00,t01,t022……tff共256个表(反引号显示不出来,见图)
需注意的是prepare bbb from @v_sql;这行代码中的@v_sql不能使用带反引号的变量名。因为这个纠结了一天。

delimiter //
CREATE procedure test()
begin
declare `@i` int (11);
declare `@j` int (11);
declare `@sqlstr` varchar (300);
declare `@abc` varchar(200);
declare `@a` varchar (11);
declare `@b` varchar (11);
set `@i` = 1;
set `@j` = 1;
while `@i` < 17 DO
    set `@a`= substring("0123456789abcdef",`@i`,1);
    while `@j` < 17 DO
           set `@b`= substring("0123456789abcdef",`@j`,1);
           select concat(`@a`,`@b`) into `@abc`;
           set @v_sql = CONCAT ("CREATE TABLE t",`@abc`,"(`id` varchar(18) NOT NULL,`id_m` varchar(32) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8");
           prepare bbb from @v_sql;
           execute bbb;
           set `@j` = `@j` + 1;
    end while;
    set `@j`= 1;
    set `@i` = `@i` + 1;
end while;
end;
call test();
drop procedure test;
//

此图是最后的代码,可以看到上面定义的变量都是带反引号的,但是如果@v_sql也提前定义并且带反引号就会报MySQL server version for the right syntax to use near ‘NULL’ at line 1。即SQL语句错误。
这里写图片描述


至于批量导入txt文件,因为load命令不知道prepared。本来想使用以下过程函数

delimiter //
CREATE procedure testtt1()
BEGIN
declare `@i` int (11);
declare `@j` int (11);
declare `@abc` varchar(200);
declare `@a` varchar (11);
declare `@b` varchar (11);
set `@i` = 1;
set `@j` = 1;
while `@i` < 17 DO
    set `@a`= substring("0123456789abcdef",`@i`,1);
    while `@j` < 17 DO
           set `@b`= substring("0123456789abcdef",`@j`,1);
           select concat(`@a`,`@b`) into `@abc`;
           set @sqlstr = CONCAT ("load data infile 'T",`@abc`,"' into table 't",`@abc`,"' fields terminated by ',' lines terminated by 'n' (id,id_m)");
           prepare bbb from @sqlstr;
           execute bbb;
           set `@j` = `@j` + 1;
    end while;
    set `@j`= 1;
    set `@i` = `@i` + 1;
end while;
END;
call testtt1();
drop procedure testtt1;
//

但是会报error: This command is not supported in the prepared statement protocol yet 。所以只能先使用别的语言生成SQL文件,再进行导入。我使用的是Java

public class CreateSql {
    final static String ALLSTR = "0123456789abcdef";

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        // String path = "d:/1992/";
        String path = sc.nextLine();
        File file = new File("d:/loadSql.sql");
        FileWriter fw = new FileWriter(file);
        StringBuffer temStr = new StringBuffer("t");
        for (int i = 0; i < 16; i++) {
            temStr.append(ALLSTR.substring(i, i + 1));
            for (int j = 0; j < 16; j++) {
                temStr.append(ALLSTR.substring(j, j + 1));
                fw.write("load data infile "" + path + temStr.toString() + ".txt"" + " into table "
                        + temStr.toString() + " fields terminated by ',' lines terminated by '\n' (`id`,`id_m`);n");
                temStr.delete(2, 3);
            }
            temStr.delete(1, 2);
        }
        fw.flush();
        fw.close();
        sc.close();
    }
}

生成SQL文件以后再执行SQL文件即可。

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_20745901/article/details/71480642
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-18 21:53:30
  • 阅读 ( 640 )
  • 分类:数据库

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢