最近花了10多天重新写了泡菜博客,采用了php5+sqlite技术。原因是mysql管理非常麻烦,而且还得花钱另外买数据库。
sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用 sqlite 存储用户数据。由此可以看出,已经没有任何理由去怀疑sqlite的稳定性了。(此段载自蓝雨设计)
那么如何在php5中使用呢?php5中有2种连接sqlite的方法。一种是默认提供的,另一种是pdo类。默认的只支持sqlite2,但是pdo可以间接支持sqlite3。下面是我写的简单的pdo类可以兼容2个版本。
以下为引用的内容:
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();
}
}
然后实例化,在实例化中如果数据库存在就自动打开,不存在就会自动创建数据库。
以下为引用的内容:
$db=new sqlite(blog.db); //这个数据库文件名字任意
创建数据库表
以下为引用的内容:
$db->query(create table test(id integer primary key,title varchar(50));
接下来添加数据
以下为引用的内容:
$db->query(insert into test(title) values(泡菜));
$db->query(insert into test(title) values(蓝雨));
$db->query(insert into test(title) values(ajan));
$db->query(insert into test(title) values(傲雪蓝天));
之后就是如何读取数据了。也就是循环。
以下为引用的内容:
$sql=select title from test order by id desc;
foreach($db->query($sql) as $rs){
echo $rs[title];
}
对于企业来说sqlite可能会小点,但是对于个人来说它确实是个好东西,可移植性非常好。
本人水平有限,如果以上内容有错误请指正。谢谢!
http://www.bkjia.com/phpjc/486566.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486566.htmltecharticle最近花了10多天重新写了泡菜博客,采用了php5+sqlite技术。原因是mysql管理非常麻烦,而且还得花钱另外买数据库。 sqlite 是一款轻量级的、...