Home > Backend Development > PHP Tutorial > php SQLite operation class implemented based on PDO (add, delete, modify, query, transaction)

php SQLite operation class implemented based on PDO (add, delete, modify, query, transaction)

怪我咯
Release: 2023-03-12 15:52:01
Original
1695 people have browsed it

This article mainly introduces the SQLite operation class implemented by PHP based on PDO, including operation implementation techniques and usage methods such as addition, deletion, modification, query, and transactions. Friends in need can refer to the following

The examples of this article describe the PHP-based SQLite operation class implemented by PDO. Share it with everyone for your reference, the details are as follows:

Direct code:

Note: Be sure to write the database save path

<?php
// sqlite分页类
class SqliteDB{
  public function construct(){
    // 初始化数据库,并且连接数据库 数据库配置
    $this->db = new PDO(&#39;sqlite:&#39;.dirname(FILE).&#39;\log.db&#39;);
    $this->table_name=$tab;
    $this->tab_init();
  }
  public function tab_init()
  {
    # 表初始化,创建表
    $this->db->exec("CREATE TABLE log(
      id integer PRIMARY KEY autoincrement,
      urls varchar(200),
      ip varchar(200),
      datetimes datetime default (datetime(&#39;now&#39;, &#39;localtime&#39;))
      )");
  }
  public function insert($tab_name,$key_list,$value_list)
  {
    // echo "INSERT INTO ".$tab_name." (".$key_list.") values(".$value_list.")";
    $result=$this->db->exec("INSERT INTO ".$tab_name." (".$key_list.") values(".$value_list.")");
    if (!$result) {
      return false;
    }
    // echo "{{{INSERT INTO ".$tab_name." (".$key_list.") values(".$value_list.")}}}}";
    $res=$this->db->beginTransaction();//事务回gun
  }
  public function total($tab_name,$tj=&#39;&#39;)//求总记录数目
  {
    $sth = $this->db->prepare(&#39;SELECT count(id) as c FROM &#39;.$tab_name.&#39; &#39;.$tj);
    $sth->execute();
    $result = $sth->fetchAll();
    return $result[0][&#39;c&#39;];
  }
  public function update()
  {
    # 修改
  }
  function delete($value=&#39;&#39;)
  {
    # 删除
  }
  public function query($tab_name,$tj=&#39;&#39;)//表名称和条件
  {
    $sth = $this->db->prepare(&#39;SELECT * FROM &#39;.$tab_name.&#39; &#39;.$tj);
    // echo &#39;SELECT * FROM &#39;.$tab_name.&#39; &#39;.$tj;
    $sth->execute();
    $result = $sth->fetchAll();
    return $result;
  }
}
// $db=new SqliteDB();
// $res=$db->insert(&#39;log&#39;,&#39;ip,urls,datetimes&#39;,&#39;"127.0.0.1","www.baidu.com","2012-12-12 00:00:00"&#39;);//添加案例
// $res=$db->query(&#39;log&#39;);//查询案例
// $res=$db->total(&#39;log&#39;);//查询案例
// print_r($res);
// foreach ($res as $key => $row) {
// echo $row[&#39;urls&#39;];
// }
?>
Copy after login

The above is the detailed content of php SQLite operation class implemented based on PDO (add, delete, modify, query, transaction). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template