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 have used it in their desktop software SQLite is also used 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 Lanyu 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{
$this->Connection=new PDO('sqlite2:'.$file);
}catch(PDOException $e){
try{
$this->Connection=new PDO('sqlite:'.$file);
}catch(PDOException $e){
exit('error!');
}
}
}
function __destruct(){
$this->Connection=null;
}
function Query($SQL){
return $this->Connection->Query($SQL);
}
function Execute($SQL){
return $this->Query($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)");
Next add data
The following is the quoted content:
$DB->Query("insert into test(title) values('Kimchi')");
$DB->Query("insert into test(title) values('Blue Rain')");
$DB->Query("insert into test(title) values('Ajan')");
$DB->Query("insert into test(title) values('Aoxuelantian')");
Then it’s time 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. Thanks!