How to easily import multiple sql files into a MyS

2019-03-08 01:47发布

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 database, click import, select a file and import it. When I have more than a couple of files it takes a long time.

I would like to know if there is a better way to import multiple files, something like one file which will import the other files or similar.

I'm using WAMP and I would like a solution that does not require installing additional programs on my computer.

标签: mysql import
8条回答
爷的心禁止访问
2楼-- · 2019-03-08 02:18

The easiest solution is to copy/paste every sql files in one.

You can't add some sql markup for file importation (the imported files will be in your computer, not in the server, and I don't think MySQL manage some import markup for external sql files).

查看更多
唯我独甜
3楼-- · 2019-03-08 02: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;

查看更多
叼着烟拽天下
4楼-- · 2019-03-08 02:23

Thanks BlackCharly, I found how to do it in Windows, open a terminal go to 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, as BlackCharly says:

cat *.sql  > all_files.sql

Last but not least, please, take into account the @mosh comment:

If all_files.sql is already present, this can result in recursive copy of the file on itself with no end! Put the result in a separate directory to be safe.

查看更多
forever°为你锁心
5楼-- · 2019-03-08 02:23

I know it's been a little over two years... but I was looking for a way to do this, and wasn't overly happy with the solution posted (it works fine, but I wanted a little more information as the import happens). When combining all the SQL files in to one, you don't get any sort of progress updates.

So I kept digging for an answer and thought this might be a good place to post what I found for future people looking for the same answer. Here's a command line in Windows that will import multiple SQL files from a folder. You run this from the command line while in the directory where mysql.exe is located.

for /f %f in ('dir /b <dir>\<mask>') do mysql --user=<user> --password=<password> <dbname> < <dir>\%f

With some assumed values (as an example):

for /f %f in ('dir /b c:\sqlbackup\*.sql') do mysql --user=mylogin --password=mypass mydb < c:\sqlbackup\%f

If you had two sets of SQL backups in the folder, you could change the *.sql to something more specific (like mydb_*.sql).

查看更多
三岁会撩人
6楼-- · 2019-03-08 02:24
  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

查看更多
Rolldiameter
7楼-- · 2019-03-08 02:36

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 .

查看更多
登录 后发表回答