Home  >  Article  >  Backend Development  >  A brief summary of using Sqlite database development in PHP5 environment_PHP tutorial

A brief summary of using Sqlite database development in PHP5 environment_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:20787browse

I recently spent more than 10 days rewriting the Kimchi blog, using PHP5+SQLITE technology. The reason is that MYSQL management is very troublesome, and you have to spend money to buy an additional database.

SQLite is a lightweight, file-based embedded database that was born in 2000. After more than 7 years of development, it has become the most popular embedded database today. Companies including Google are using Its desktop software also uses sqlite to store user data. It can be seen from this that there is no reason to doubt the stability of SQLite. (This paragraph is from Blue Rain Design)

So how to use it in PHP5? There are 2 ways to connect to sqlite in PHP5. One is provided by default and the other is the PDO class. The default only supports sqlite2, but PDO can indirectly support sqlite3. Below is a simple PDO class I wrote that is compatible with 2 versions.


The following is the quoted content:
class SQLite{
function __construct($file){
try{
Connection=new PDO(sqlite2:.$ file);
}catch(PDOException $e){
            try{
                   $this->Connection=new PDO(sqlite:.$file);
                                                                                                                      }
function Query ($SQL){
                                                                                         SQL)->fetch();
}
function RecordArray($SQL){
return $this->Query($SQL)->fetchAll();
}
function RecordCount($SQL){
return count($this->RecordArray($SQL));
}
function RecordLastID(){
return $this->Connection-> ;lastInsertId();
}
}


Then instantiate. During instantiation, if the database exists, it will be automatically opened. If it does not exist, the database will be automatically created.


The following is the quoted content:
$DB=new SQLite(blog.db); //The name of this database file is arbitrary


Create database table

The following is the quoted content:

$DB->Query("create table test(id integer primary key,title varchar(50)");



Add data next

The following is the quoted content:

$DB->Query("insert into test(title) values(pickle)");

$DB->Query("insert into test (title) values(Lanyu)");
$DB->Query("insert into test(title) values(Ajan)");
$DB->Query("insert into test( title) values(Aoxuelantian)");

The next step is how to read the data. That is the cycle.


The following is the quoted content:
$SQL=select title from test order by id desc;
foreach($DB->Query($SQL) as $RS){
echo $RS[title];
}

SQLITE may be smaller for enterprises, but it is really a good thing for individuals, and its portability is very good.

My level is limited, please correct me if there are any errors in the above content. Thank you!





http://www.bkjia.com/PHPjc/486566.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/486566.html

TechArticle

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn