In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1) .MySQLimport syntax introduction:
MySQLimport is located in the MySQL/bin directory and is a very effective tool for MySQL to load (or import) data. This is a command line tool. There are two parameters and a large number of options to choose from. This tool imports a text file (text file) into the database and table you specify. For example, we want to import the data from the file Customers.txt into the table Custermers in the database Meet_A_Geek:
MySQLimport Meet_A_Geek Customers.txt
Note: here Customers.txt is the text file where we want to import the data, and Meet_A_Geek is the database we want to operate, and the table name in the database is Customers, where the data format of the text file must be consistent with the record format in the Customers table, otherwise the MySQLimport command will make an error.
The name of the table is the file string before the first period (.) of the import file, and another example: MySQLimport Meet_A_Geek Cus.to.mers.txt
Then we will import the contents of the file into the Cus table in the database Meet_A_Geek.
In the above example, only two parameters are used, and no more options are used. Here are the options for MySQLimport.
2) introduction to the common options of .MySQLimport:
Option function
-d or-- delete delete all information in the data table before the new data is imported into the data table
-- for-- force will force data insertion to continue regardless of whether an error is encountered or not
-I or-- ignore MySQLimport skips or ignores lines with the same unique keyword, and the data in the import file is ignored.
-l or-lock-tables data locks the table before it is inserted, which prevents users' queries and updates from being affected when you update the database.
The-r or-replace option is the opposite of the-I option; this option replaces the record with the same unique keyword in the representative.
-- fields-enclosed- by= char specifies what is enclosed when recording data in a text file, and in many cases the data is enclosed in double quotes. By default, the data is not enclosed in characters.
-- fields-terminated- by=char specifies the delimiter between the values of each data. In a period-delimited file, the delimiter is a period. You can use this option to specify a separator between data.
The default delimiter is the jumper (Tab)
-- lines-terminated- by=str this option specifies the delimited string or character of the data between lines in the text file. By default, MySQLimport uses newline as the line delimiter.
You can choose to replace a single character with a string:
A new line or a carriage return.
Other common options for the MySQLimport command are-v display version (version),-p prompt for password (password), and so on.
3)。 Example: import a file with a comma delimiter
The record format of the lines in the file is as follows:
"1", "ORD89876", "1 Dozen Roses", "19991226"
Our task is to import the data from this file into the table Orders in the database Meet_A_Geek. We use this command:
Bin/MySQLimport-prl-fields-enclosed-by= "- fields-terminated-by=, Meet_A_Geek Orders.txt
This command may seem uncomfortable, but when you get familiar with it, it's very simple. The first part, bin/MySQLimport, tells the operating system that the command you want to run is MySQLimport under the MySQL/bin directory. Option p requires you to enter a password, which requires you to enter a password before changing the database. We use the r option because we want to replace the unique keyword in the table with the record in the file record with the data in the file. The data in our form is not up-to-date and needs to be updated with the data in the file, so we use the option r to replace the records already in the database. The function of the l option is to lock the table when we insert the data, which prevents users from querying or changing the table when we update the table.
Batch processing is a non-interactive way to run MySQL programs, and you will still use these commands just like the commands you use in MySQL.
To implement batch processing, if you redirect a file into the MySQL program, we first need a text file that contains the same text as the command we entered in MySQL.
For example, if we want to insert some data, use a file that contains the following text (the file name is New_Data.sql, of course, we can also name it New_Data.txt and any other legal name, not necessarily with the end of sql):
USE Meet_A_Geek
INSERT INTO Customers (Customer_ID, Last_Name) VALUES (NULL, "Block")
INSERT INTO Customers (Customer_ID, Last_Name) VALUES (NULL, "Newton")
INSERT INTO Customers (Customer_ID, Last_Name) VALUES (NULL, "Simmons")
Note that the syntax of the above sentences must be correct and each sentence ends with a semicolon.
The above USE command selects the database, and the INSERT command inserts the data.
Next, we want to import the above file into the database, and before importing, make sure that the database is already running, that is, the MySQLd process (or service, called "service" below Windows NT, and "process" under unix) is already running.
Then run the following command:
Bin/MySQL-p
< /home/mark/New_Data.sql 接着按提示输入密码,如果上面的文件中的语句没有错误,那么这些数据就被导入到了数据库中。 命令行中使用LOAD DATA INFILE 从文件中导入数据到数据库: 现在您可能会问自己,"究竟为什么我要输入所有的这些SQL语句到文件中,然后通过程序运行它们呢?" 这样看起来好像需要大量的工作。很好,你这样想很可能就对了。但是假如你有从所有这些命令中产生的log记录呢?现在这样就很棒,嗯,大多数数据库都会自动产生数据库中的事件记录的log。而大部分log都包含有用过的原始的SQL命令。因此,如果您不能从您现在的数据库中导出数据到新的MySQL数据库中使用,那么您可以使用log和MySQL的批处理特性,来快速且方便地导入您地数据。当然,这样就省去了打字的麻烦。 LOAD DATA INFILE 这是我们要介绍的最后一个导入数据到MySQL数据库中的方法。这个命令与MySQLimport非常相似,但这个方法可以在MySQL命令行中使用。也就是说您可以在所有使用API的程序中使用这个命令。使用这种方法,您就可以在应用程序中导入您想要导入的数据。 使用这个命令之前,MySQLd进程(服务)必须已经在运行。 启动MySQL命令行: bin/MySQL -p 按提示输入密码,成功进入MySQL命令行之后,输入下面的命令: USE Meet_A_Geek; LOAD DATA INFILE "/home/mark/data.sql" INTO TABLE Orders; 简单的讲,这样将会把文件data.sql中的内容导入到表Orders中,如MySQLimport工具一样,这个命令也有一些可以选择的参数。比如您需要把自己的电脑上的数据导入到远程的数据库服务器中,您可以使用下面的命令: LOAD DATA LOCAL INFILE "C:MyDocsSQL.txt" INTO TABLE Orders; 上面的LOCAL参数表示文件是本地的文件,服务器是您所登陆的服务器。 这样就省去了使用ftp来上传文件到服务器,MySQL替你完成了. 您也可以设置插入语句的优先级,如果您要把它标记为低优先级(LOW_PRIORITY),那么MySQL将会等到没有其他人读这个表的时候,才把插入数据。可以使用如下的命令: LOAD DATA LOW_PRIORITY INFILE "/home/mark/data.sql" INTO TABLE Orders; 您也可以指定是否在插入数据的时候,取代或者忽略文件与数据表中重复的键值。替代重复的键值的语法: LOAD DATA LOW_PRIORITY INFILE "/home/mark/data.sql" REPLACE INTO TABLE Orders; 上面的句子看起来有点笨拙,但却把关键字放在了让您的剖析器可以理解的地方。 下面的一对选项描述了文件的记录格式,这些选项也是在MySQLimport工具中可以用的。他们在这里看起来有点不同。首先,要用到FIELDS关键字,如果用到这个关键字,MySQL剖析器希望看到至少有下面的一个选项: TERMINATED BY character ENCLOSED BY character ESCAPED BY character 这些关键字与它们的参数跟MySQLimport中的用法是一样的. The TERMINATED BY 描述字段的分隔符,默认情况下是tab字符(t) ENCLOSED BY描述的是字段的括起字符。比方以引号括起每一个字段。 ESCAPED BY 描述的转义字符。默认的是反些杠(backslash: ). 下面仍然使用前面的MySQLimport命令的例子,用LOAD DATA INFILE语句把同样的文件导入到数据库中: LOAD DATA INFILE "/home/mark/Orders.txt" REPLACE INTO TABLE Orders FIELDS TERMINATED BY ',' ENCLOSED BY '"'; LOAD DATA INFILE语句中有一个MySQLimport工具中没有特点: LOAD DATA INFILE 可以按指定的列把文件导入到数据库中。 当我们要把数据的一部分内容导入的时候,这个特点就很重要。比方说,我们要从Access数据库升级到MySQL数据库的时候,需要加入一些栏目(列/字段/field)到MySQL数据库中,以适应一些额外的需要。 这个时候,我们的Access数据库中的数据仍然是可用的,但是因为这些数据的栏目(field)与MySQL中的不再匹配,因此而无法再使用MySQLimport工具。尽管如此,我们仍然可以使用LOAD DATA INFILE,下面的例子显示了如何向指定的栏目(field)中导入数据: LOAD DATA INFILE "/home/Order.txt" INTO TABLE Orders(Order_Number, Order_Date, Customer_ID); 如您所见,我们可以指定需要的栏目(fields)。这些指定的字段依然是以括号括起,由逗号分隔的,如果您遗漏了其中任何一个,MySQL将会提醒您^_^ Importing Data from Microsoft Access (从Access中导入数据,略) 您可以看到MySQL有很多可以导入数据的方法,然而这些只是数据传输中的一半。另外的一般是从MySQL数据库中导出数据。有许多的原因我们需要导出数据。一个重要的原因是用于备份数据库。数据的造价常常是昂贵的,需要谨慎处理它们。经常地备份可以帮助防止宝贵数据地丢失;另外一个原因是,也许您希望导出数据来共享。 在这个信息技术不断成长的世界中,共享数据变得越来越常见。 比方说Macmillan USA维护护着一个将要出版的书籍的大型数据库。这个数据库在许多书店之间共享,这样他们就知道哪些书将会很快出版。医院越来越走向采用无纸病历记录,这样这些病历可以随时跟着你。世界变得越来越小,信息也被共享得越来越多。有很多中导出数据得方法,它们都跟导入数据很相似。因为,毕竟,这些都只是一种透视得方式。从数据库导出的数据就是从另一端导入的数据。这里我们并不讨论其他的数据库各种各样的导出数据的方法,您将学会如何用MySQL来实现数据导出。 使用MySQLdump: MySQLdump命令位于MySQL/bin/目录中 MySQLdump工具很多方面类似相反作用的工具MySQLimport。它们有一些同样的选项。但MySQLdump能够做更多的事情。它可以把整个数据库装载到一个单独的文本文件中。这个文件包含有所有重建您的数据库所需要的SQL命令。这个命令取得所有的模式(Schema,后面有解释)并且将其转换成DDL语法(CREATE语句,即数据库定义语句),取得所有的数据,并且从这些数据中创建INSERT语句。这个工具将您的数据库中所有的设计倒转。因为所有的东西都被包含到了一个文本文件中。这个文本文件可以用一个简单的批处理和一个合适SQL语句导回到MySQL中。这个工具令人难以置信地简单而快速。决不会有半点让人头疼地地方。 因此,如果您像装载整个数据库Meet_A_Geek的内容到一个文件中,可以使用下面的命令: bin/MySQLdump -p Meet_A_Geek >MeetAGeek_Dump_File.txt
This statement also allows you to specify a table for dump (backup / export / load?). If you just want to export the entire contents of the table Orders in the database Meet_A_Geek to a file, you can use the following command:
Bin/MySQLdump-p Meet_A_Geek Orders > MeetAGeek_Orders.txt
This is so flexible that you can even use the WHERE clause to select the records you want to export to a file. To do this, use a command similar to the following:
Bin/MySQLdump-p-where= "Order_ID > 2000" Meet_A_Geek Orders > Special_Dump.txt
The MySQLdump tool has a large number of options, some of which are shown in the table below:
Option / Option role / Action Performed
-- add-drop-table
This option will precede each table with a DROP TABLE IF EXISTS statement, which ensures that there will be no error when importing back to the MySQL database, because each time it is imported, it will first check whether the table exists and then delete it.
-- add-locks
This option binds a LOCK TABLE and UNLOCK TABLE statement to the INSERT statement. This prevents other users from operating on the table when these records are re-imported into the database
-c or-complete_insert
This option causes the MySQLdump command to add the name of the column (field) to each generating INSERT statement. This option is useful when exporting data to another database.
-- delayed-insert adds the DELAY option to the INSERT command
-F or-flush-logs uses this option to refresh the log of the MySQL server before performing the export.
-for-force uses this option to continue exporting even if an error occurs
-- full this option adds additional information to the CREATE TABLE statement
-l or-lock-tables uses this option, and the server will lock the table when it is exported.
-t or-no-create- info
This option prevents the MySQLdump command from creating CREATE TABLE statements, which is convenient when you only need data and not DDL (database definition statements).
The-d or-no-data option causes the MySQLdump command not to create INSERT statements.
You can use this option when you only need DDL statements.
-- opt this option opens all options that increase file export speed and create a file that can be imported faster.
The-Q or-quick option allows MySQL not to read the entire export into memory and then perform the export, but to write it to the import file when it is read.
The option-T path or-tab = path creates two files, one containing DDL statements or table creation statements, and the other containing data. The DDL file is named table_name.sql and the data file is named table_name.txt. The pathname is the directory where the two files are stored. The directory must already exist and the user of the command has privileges on the file.
-w "WHERE Clause" or-where = "Where clause"
As mentioned earlier, you can use this option to filter the data that will be placed in the exported file.
Suppose you need to create a file for the account to be used in a form, and the manager wants to look at all the orders (Orders) for this year (2004), which are not interested in DDL and need to be comma-delimited because they can be easily imported into Excel. To complete this character, you can use the following sentence:
Bin/MySQLdump-p-where "Order_Date > = '2000-01-01'"
-tab = / home/mark-no-create-info-fields-terminated-by=, Meet_A_Geek Orders
This will get the results you want.
Schema: mode The set of statements, expressed in data definition language, that completely describe the structure of a data base.
A set of statements expressed in a data definition language that fully describes the structure of the database.
SELECT INTO OUTFILE:
If you don't think the MySQLdump tool is cool, use SELECT INTO OUTFILE. MySQL also provides a command that is the opposite of the LOAD DATA INFILE command, which is the SELECT INTO OUTFILE command, and there are many similarities between the two commands. First of all, they have almost the same options. Now that you need to complete the previous functions done with MySQLdump, you can follow these steps:
1. Make sure the MySQLd process (service) is already running
2. Cd / usr/local/MySQL
3. Bin/MySQLadmin ping; / / if this sentence fails, you can use this: MySQLadmin-u root-p ping
MySQLadmin ping is used to detect the status of MySQLd, is alive indicates that it is running, and a user name and password may be required for errors.
4. Start the MySQL listener.
5. Bin/MySQL-p Meet_A_Geek;// enters the MySQL command line and opens the database Meet_A_Geek. You need to enter a password.
6. On the command line, enter the following command:
SELECT * INTO OUTFILE'/ home/mark/Orders.txt'
FIELDS
TERMINATED BY =','
FROM Orders
WHERE Order_Date > = '2000-01-01'
After you press Return, the file is created. This sentence is like a regular SELECT statement, only redirecting the output of the screen to the file. This means that you can use JOIN to implement advanced queries for multiple tables. This feature can also be used as a report generator.
For example, you can combine the methods discussed in this chapter to produce a very interesting query. Try this:
Create a text file called Report_G.rpt in the MySQL directory and add the following line:
USE Meet_A_Geek
INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
VALUES (NULL, "Kinnard", "Vicky")
INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
VALUES (NULL, "Kinnard", "Steven")
INSERT INTO Customers (Customer_ID, Last_Name, First_Name)
VALUES (NULL, "Brown", "Sam")
SELECT Last_Name INTO OUTFILE'/ home/mark/Report.rpt'
FROM Customers WHERE Customer_ID > 1
Then verify that the MySQL process is running and that you enter the following command in the MySQL directory:
Bin/MySQL
< Report_G.rpt检查您命名作为输出的文件,这个文件将会包含所有您在Customers表中输入的顾客的姓。 如您所见,您可以使用今天学到的导入/导出(import/export)的方法来帮助得到报表。[@more@] cmd->E:webmysqlbinmysqldump-user=root-password=admn
Phpblog > phpblog.sql
.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.