Php is web programming language very popular among developers. Php is actually a scripting language which means there is no need to compile the code to run. So write and run is the motto of php language. Php scripts can be run on Linux systems like bash, Perl scripts too. Some administrators with development background prefers php for scripting because php offers more than bash scripting. SQlite is an other popular database which is designed for simple, reliable and fast usage. Sqlite do not need any setup and service to run. Just SQlite library and create a database file like Microsoft Access mdb and work. In this tutorial we will look how to create simple and fast php application in bash which uses sqlite database for storing data.
Php是在开发人员中非常流行的Web编程语言。 Php实际上是一种脚本语言,这意味着无需编译代码即可运行。 因此,编写和运行是php语言的座右铭。 Php脚本也可以在bash,Perl脚本等Linux系统上运行。 一些具有开发背景的管理员更喜欢使用php编写脚本,因为php提供的功能远超过bash脚本编写。 SQlite是另一个流行的数据库,旨在简单,可靠和快速地使用。 Sqlite不需要任何设置和服务即可运行。 只需SQlite库并创建一个像Microsoft Access mdb这样的数据库文件即可工作。 在本教程中,我们将研究如何在bash中创建简单,快速的php应用程序,该应用程序使用sqlite数据库存储数据。
安装PHP和Sqlite (Install Php & Sqlite)
We will install required PHP and SQlite package with the following commands.
我们将使用以下命令安装所需PHP和SQlite软件包。
Ubuntu,Debian,薄荷 (Ubuntu, Debian, Mint)
$ sudo apt install php sqlite
Fedora,CentOS,RedHat (Fedora,CentOS, RedHat)
$ sudo yum install -y php sqlite
![Install Php & Sqlite](https://i-blog.csdnimg.cn/blog_migrate/ce7303ed3ed90d9c383a17406d8cd318.png)
测试PHP (Test Php)
Php installation can be tested like below. We will run simple php code with PHP interpreter.
可以按照以下方式测试PHP安装。 我们将使用PHP解释器运行简单的php代码。
$ php -r "echo 'Hi Poftut';"
![Test Php](https://i-blog.csdnimg.cn/blog_migrate/43745bd63efa8206049681eb478ab5ec.png)
- We can run php as above 我们可以像上面一样运行php
php is our Php interpreter command
php是我们的Php解释器命令
-r will run our code “echo ‘Hi Poftut’; ”
-r将运行我们的代码“ echo'Hi Poftut'; ”
用PHP初始化Sqlite数据库 (Initialize Sqlite Database With Php)
We will create a database file with php script. And then create a table. Our php script file name is createdb.php
我们将使用php脚本创建一个数据库文件。 然后创建一个表。 我们的php脚本文件名为createdb.php
<?php
$db = new SQLite3('mysqlitedb.db');
if($db->exec('CREATE TABLE bar (bar STRING)') ){
echo "Successfully Created";
}
else{
echo "Failure";
}
?>
Run our createdb.php script like below
运行我们的createdb.php脚本,如下所示
$ php createdb.php
Successfully Created
If we look in the current working directory we can see that there is a file named mysqlitedb.db
如果我们查看当前的工作目录,可以看到有一个名为mysqlitedb.db的文件
运行应用程序(Run Application)
Our application will insert specified parameter into our database. Our application code is resides in insertdata.php
. We insert data into table named bar
with the following script.
我们的应用程序会将指定的参数插入数据库。 我们的应用程序代码位于insertdata.php
。 我们使用以下脚本将数据插入名为bar
表中。
<?php
$db = new SQLite3('mysqlitedb.db');
if($db->exec('Insert Into bar values ("'+$argv[1]+'");') ){
echo "Successfully Created";
}
else{
echo "Failure";
}
?>
We can run our code like below
我们可以像下面那样运行我们的代码
$ php insertdata.php "hi"
Successfully Created
如何使用Php和Sqlite Infografic创建快速的简单数据库和表 (How To Create Fast Simple Database and Table With Php and Sqlite Infografic)
![How To Create Fast Simple Database and Table With Php and Sqlite Infografic](https://i-blog.csdnimg.cn/blog_migrate/8f255a3e7dafe7fdb59a247d70ad4585.png)
翻译自: https://www.poftut.com/create-fast-simple-database-table-php-sqlite/