Home  >  Article  >  Backend Development  >  PHP method to implement SQLite operation class based on PDO

PHP method to implement SQLite operation class based on PDO

墨辰丷
墨辰丷Original
2018-05-21 11:48:201385browse

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 details are as follows:

Direct code:

Note: Be sure to write the database save path

db = new PDO('sqlite:'.dirname(__FILE__).'\log.db');
    $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('now', 'localtime'))
      )");
  }
  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='')//求总记录数目
  {
    $sth = $this->db->prepare('SELECT count(id) as c FROM '.$tab_name.' '.$tj);
    $sth->execute();
    $result = $sth->fetchAll();
    return $result[0]['c'];
  }
  public function update()
  {
    # 修改
  }
  function delete($value='')
  {
    # 删除
  }
  public function query($tab_name,$tj='')//表名称和条件
  {
    $sth = $this->db->prepare('SELECT * FROM '.$tab_name.' '.$tj);
    // echo 'SELECT * FROM '.$tab_name.' '.$tj;
    $sth->execute();
    $result = $sth->fetchAll();
    return $result;
  }
}
// $db=new SqliteDB();
// $res=$db->insert('log','ip,urls,datetimes','"127.0.0.1","www.baidu.com","2012-12-12 00:00:00"');//添加案例
// $res=$db->query('log');//查询案例
// $res=$db->total('log');//查询案例
// print_r($res);
// foreach ($res as $key => $row) {
// echo $row['urls'];
// }
?>

Related recommendations:

phppdoPublic class definition and usage

##Detailed explanation of PDO operation class steps for adding, deleting, modifying and querying mysql database

php transaction processing method based on pdo

##

The above is the detailed content of PHP method to implement SQLite operation class based on PDO. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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