How to easily import multiple sql files into a MySQL database?

后端 未结 11 1770
一整个雨季
一整个雨季 2021-01-29 20:02

I have several sql files and I want to import all of them at once into a MySQL database.

I go to PHPMyAdmin, access the databa

相关标签:
11条回答
  • 2021-01-29 20:12

    In Windows, open a terminal, go to the content folder and write:

    copy /b *.sql all_files.sql
    

    This concate all files in only one, making it really quick to import with PhpMyAdmin.

    In Linux and macOS, as @BlackCharly pointed out, this will do the trick:

    cat *.sql  > .all_files.sql
    

    Important Note: Doing it directly should go well, but it could end up with you stuck in a loop with a massive output file getting bigger and bigger due to the system adding the file to itself. To avoid it, two possible solutions.

    A) Put the result in a separate directory to be safe (Thanks @mosh):

    mkdir concatSql
    cat *.sql  > ./concatSql/all_files.sql
    

    B) Concat them in a file with a different extension and then change it the name. (Thanks @William Turrell)

    cat *.sql  > all_files.sql1
    mv all_files.sql1 all_files.sql
    
    0 讨论(0)
  • 2021-01-29 20:12

    You could also a for loop to do so:

    #!/bin/bash
    for i in *.sql
    do
        echo "Importing: $i"
        mysql your_db_name < $i
        wait
    done 
    

    Source

    0 讨论(0)
  • 2021-01-29 20:13

    Save this file as .bat and run it , change variables inside parenthesis ...

    @echo off
    title Mysql Import Script
    cd (Folder Name)
     for %%a in (*) do (
         echo Importing File  : %%a 
         mysql -u(username) -p(password)  %%~na < %%a
    )
    pause
    

    if it's only one database modify (%%~na) with the database name .

    0 讨论(0)
  • 2021-01-29 20:20

    Enter the mysql shell like this.

    mysql --host=localhost --user=username --password --database=db

    Then use the source command and a semicolon to seperate the commands.

    source file1.sql; source file2; source file3;

    0 讨论(0)
  • 2021-01-29 20:21
    1. Goto cmd

    2. Type in command prompt C:\users\Usersname>cd [.sql tables folder path ]
      Press Enter
      Ex: C:\users\Usersname>cd E:\project\database

    3. Type command prompt
      C:\users\Usersname>[.sql folder's drive (directory)name]
      Press Enter
      Ex: C:\users\Usersname>E:

    4. Type command prompt for marge all .sql file(table) in a single file
      copy /b *.sql newdatabase.sql
      Press Enter
      EX: E:\project\database>copy /b *.sql newdatabase.sql

    5. You can see Merge Multiple .sql(file) tables Files Into A Single File in your directory folder
      Ex: E:\project\database

    0 讨论(0)
  • 2021-01-29 20:24

    Just type below command on your command prompt & it will bind all sql file into single sql file,

    c:/xampp/mysql/bin/sql/ type *.sql > OneFile.sql;
    
    0 讨论(0)
提交回复
热议问题