小技巧:用批处理对MySQL进行数据操作_MySQL
批处理是一种非交互式运行mysql程序的方法,如同您在mysql中使用的命令一样,你仍然将使用这些命令。
为了实现批处理,您重定向一个文件到mysql程序中,首先我们需要一个文本文件,这个文本文件包含有与我们在mysql中输入的命令相同的文本。
比如我们要插入一些数据,使用包含下面文本的文件(文件名为New_Data.sql,当然我们也可以取名为New_Data.txt及任何其他的合法名字,并不一定要以后缀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");
注意上面的这些句子的语法都必须是正确的,并且每个句子以分号结束。
上面的USE命令选择数据库,INSERT命令插入数据。
下面我们要把上面的文件导入到数据库中,导入之前要确认数据库已经在运行,即是mysqld进程(或者说服务,Windows NT下面称为”服务“,unix下面为”进程“)已经在运行。
然后运行下面的命令:
bin/mysql –p 接着按提示输入密码,如果上面的文件中的语句没有错误,那么这些数据就被导入到了数据库中。
命令行中使用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:\MyDocs\SQL.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中导入数据,略)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Llama3, the majestic king of open source, the original context window is only... 8k, which makes me swallow back the words "it smells so good". Today, when 32k is the starting point and 100k is common, is this intentional to leave room for contributions to the open source community? The open source community certainly didn't miss this opportunity: now with just 58 lines of code, any fine-tuned version of Llama370b can automatically scale to 1048k (one million) contexts. Behind the scenes is a LoRA, extracted from a fine-tuned version of Llama370BInstruct that extends good context, and the file is only 800mb. Next, using Mergekit, you can run it with other models of the same architecture or merge it directly into the model. 1048k context used

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs
