Development of SQLite Database in PHP5 Environment Small summary _php tutorial

Source: Internet
Author: User

Recently spent more than 10 days to re-write the kimchi blog, using the Php5+sqlite technology. The reason is that MySQL management is cumbersome, and it has to pay for a separate database.

SQLite is a lightweight, file-based embedded database, was born in 2000, after more than 7 years of development, until today has become the most popular embedded database, including Google, the company in its desktop software also uses SQLite to store user data. As you can see, there is no reason to doubt the stability of SQLite. (This section is from Lan Yu design)

So how do you 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 support sqlite3 indirectly. The following is a simple PDO class that I wrote to be compatible with 2 versions.


The following is the referenced 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 ();
}
}

The database is then instantiated and automatically created if the database exists in the instantiation, and does not exist.


The following is the referenced content:
$DB =new SQLite (blog.db); This database file name is arbitrary

Create a database table


The following is the referenced content:
$DB->query ("CREATE TABLE test (ID integer primary key,title varchar (50)");

Next Add the data


The following is the referenced content:
$DB->query ("INSERT INTO Test" values (kimchi));
$DB->query ("INSERT into test (title) VALUES (Blue Rain)");
$DB->query ("INSERT into Test (Ajan)");
$DB->query ("INSERT into test (title) VALUES (Proud snow blue sky)");

Then it's how to read the data. That is, loops.


The following is the referenced content:
$SQL =select title from Test order by id DESC;
foreach ($DB->query ($SQL) as $RS) {
echo $RS [title];
}

SQLite may be a little bit small for businesses, but it's a good thing for an individual, and portability is great.

My level is limited, if the above content is wrong please correct me. Thank you!

http://www.bkjia.com/PHPjc/486566.html www.bkjia.com true http://www.bkjia.com/PHPjc/486566.html techarticle recently spent more than 10 days to re-write the kimchi blog, using the Php5+sqlite technology. The reason is that MySQL management is cumbersome, and it has to pay for a separate database. SQLite is a lightweight 、...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.