php实例之创建SQLite数据库

发布时间:2020-09-06编辑:脚本学堂
本文分享一个php实例,使用pdo创建一个sqlite数据库,有需要的朋友不妨作个参考。

例子:
 

复制代码 代码示例:

<?php
/**
* 创建sqlite数据库
* by www.jb200.com
*/
$db = new PDO('sqlite:/usr/local/zodiac');

$db->beginTransaction();
$q = $db->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'zodiac'");

if ($q->fetch() === false) {

    $db->exec(<<<_SQL_
CREATE TABLE zodiac (
  id INT UNSIGNED NOT NULL,
  sign CHAR(11),
  symbol CHAR(13),
  planet CHAR(7),
  element CHAR(5),
  start_month TINYINT,
  start_day TINYINT,
  end_month TINYINT,
  end_day TINYINT,
  PRIMARY KEY(id)
)
_SQL_
);

    $sql=<<<_SQL_
INSERT INTO zodiac VALUES (1,'Aries','Ram','Mars','fire',3,21,4,19);
INSERT INTO zodiac VALUES (2,'Taurus','Bull','Venus','earth',4,20,5,20);
INSERT INTO zodiac VALUES (3,'Gemini','Twins','Mercury','air',5,21,6,21);
_SQL_;

    foreach (explode("n",trim($sql)) as $q) {
        $db->exec(trim($q));
    }
    $db->commit();
} else {
    $db->rollback();
}
?>