MySQL & Perl, 便利之合_MySQL
Perl
by Georges Tarbouriech
What about this nice pair ?
MySQL是一种关系型数据库系统(RDBMS),其主页为http://www.mysql.com/。它由GNU GPL发布并供免费使用,请注意阅读版权声明。 它可以在许多平台上工作,既作服务器同时又是客户端。除MySQL之外还有一些其它的自由软件性质的RDBMS,在这里不作任何比较,本文仅讨论MYSQL。我们也不将其与那些大型商业数据库像 Informix,Oracle,Sybase等作比,有足够理由相信MySQL是Internet上使用最广泛的数据库系统之一。在这篇文章里我们使用的MySQL版本是3.23.36。现在的稳定发行版本是3.23.46,而苦苦等待的4.0版仍在测试之中。人们可以从网上下载到它们的源代码或包文件。
为了将MySQL与Perl结合使用,你还需要些东西: Perl DBI模块。至少你应该下载DBI, Msql-Mysql-modules, Data-Dumper和Data-ShowTable等。
本文不介绍它们的安装过程,因为那很简单,同时包中的说明文件已经提供了你需要知道的所有内容。
Perl全称是实用摘录与报告语言(Practical Extraction and Report Language)。 最初它用于文件处理(分析,摘要...),很快它的功能就有了扩展。 你几乎可以用它来做任何任何事情:系统管理,CGI脚本程序以及数据库接口程序。
Perl包含于许多(如果不能称是全部的话)Unix发行版本中,它们中有些是免费的,有些不是。本文写作时,稳定的版本是5.6.1,版本5.7.2在测试中。本文采用的是5.005_03,很不错的一个版本,尽管老点。如果你的机子还没有装Perl,你可以从http://www.perl.com/下载。Perl提供了许多模块,用它们几乎可以完成任何事,你可以从这个网站的CPAN栏目找到它们。
最后一点,为了让这两种软件真正开始工作,你还需要一个web服务器。Apache应该是一个不错的选择,它集成于多种Unix系统中。如果你还没有,可以到http://www.apache.org/下载。
使用的范例
你也许已经注意到LinuxFocus杂志有多种语言的版本。这就意味着作为编辑需要同时管理新文章以及它的译文版本。一般情况下,我们可以看到大约200篇文章,平均每篇文章有5个语言的版本,这样产生了大约1000篇文章并且还在继续增长!这些文章需要被存档,格式标准化,总结及摘要.....应该怎么做这些事?当然,用Perl!
我们的总编Guido Socher编写的许多perl程序使我们的工作变的简单了许多,他写过一本三部头的 Perl教程和一本评论Perl的书。参照本文末尾的参考文献部分。
Javi,我们的西班牙编辑,用Perl编写了一个程序来管理翻译进度。
Atif是我们的明星作者,他来自perl王国,所以他的母语就是Perl。 他同时也撰写关于MySQL方面文章,致力于一个WEB管理工具改进工作。同样你可以在参考文献部分找到他。
总之,如果你在寻找一个Perl世界,加入LinuxFocus。
我是LinuxFocus法文版的编辑之一,我更懒,于是创建了自己的LinuxFocus数据库,猜猜用什么: MySQL 和Perl!
建立数据库
首先你应当已经正确安装了MySQL,并配置好用户密码。关于安装并不是本文讨论的内容,MySQL自带的大量文档已经描述了所有细节。
用mysql.server启动MySQL服务器,这个命令同时调用 safe_mysqld 守护进程,因此你可以给它传参。
用
mysql -h host -u user -p
连接到服务器,如果服务器就装在你本机上,就不用加参数-h host。
输入密码无误后,你将连接到服务器。现在可以建立自己的数据库了。
在mysql命令提示符状态下输入
CREATE DATABASE lf;
,这个是我们的示范数据库(lf代表LinuxFocus),你可以根据你的需要命名成别的。接下来就是给用户授权了,当然首先你要有足够的权限(你需要用有administrator权限的用户连接)。如果需要让某个用户管理数据库,通过
GRANT ALL ON lf.* TO username;
给他授权。输入
USE lf
选择刚才创建的数据库,并创建一张表。在这里我们创建的表是trissue,命令格式为:
CREATE TABLE trissue (num INTEGER UNSIGNED, category VARCHAR(25), title VARCHAR(40), author VARCHAR(20), en VARCHAR(20), es VARCHAR(20), fr VARCHAR(20),de VARCHAR(20), nl VARCHAR(20), ru VARCHAR(20), tk VARCHAR(20), issue VARCHAR(20));
通过下面的命令可以检查一下我们刚才创建的表内容是否正确
USE lf
SHOW TABLES;
DESCRIBE trissue;
下面我们需要在表中填入数据,往一张空表中导入数据的最简单的方法就是使用一个带TAB分隔符的文本文件。如果文本文件已经准备好,输入
LOAD DATA LOCAL INFILE "maindb.txt" INTO TABLE trissue;
如果你的文本文件没有问题,那么现在这张表就已经填好数据,你可以通过输入以下命令检验一下:
SELECT * FROM trissue;
这将导致显示一个很长的列表。现在,你就可以进行查询来获得任何类型的数据了。
ok,到现在为止,我们仅仅用了MySQL,就可以做任何事情,那么,用Perl来做什么?
Perl的工作
Perl可以帮助我们自动进行查询,将结果显示到一个WEB浏览器上,等等。重复一遍,首先需要为Perl安装正确的模块使之能与MYSQL联合工作。
现在我们用Perl来写一个CGI脚本。它的作用是将Perl与HTML技术结合以实现查询数据库并将结果格式化输出。
我们用一个简单的脚本来查询某一作者的所有文章,显示文章的编号,分类,标题,不同语言版本的翻译者的姓名,发表文章的杂志期号。
你可以将这个脚本当作一个模块使用,但是注意这个例程并不是一个非常可靠的程序。你可以从下面的链接下载到一个有详细注释的版本。=>here
#!/usr/bin/perl -Tw
# First, we say this is a "Tainted" Perl script.
#
# This is a comment
# db consult
#
# We use the Perl DBI module
use DBI;
# As cgi :
use CGI qw(param());
Content-type: text/html
|
下面用脚本去查询数据库。
Search by author
END_of_start
if (param("author") ne '') {
$author = param("author");
$autsrch.='"';
$autsrch.=$author;
$autsrch.='"';
# We connect to the database named lf as user doe
$dbh = DBI->connect("DBI:mysql:lf","doe",'');
$sth = $dbh->prepare("
select *
from trissue
where
author = $autsrch
");
$sth->execute;
接着用脚本去显示查询结果。如果我们不限制查询条件,将会显示出数据库的所有内容,如果我们提供一个作者姓名,则会显示出与该作者相关的所有文章。当你的数据库有上千条记录时,不推荐显示出所有内容!
Num | Category | Title | Author | En | Es | Fr | De | Nl | Ru | Tk | Issue |
---|---|---|---|---|---|---|---|---|---|---|---|
$num | ";$category | ";$title | ";$author | ";$e |

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











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...
