search
Homephp教程PHP源码封装的Sqlite3简易操作类

封装的Sqlite3简易操作类  

封装的Sqlite3简易操作类,基于Sqlite3扩展写的,用法和前一个Mysql一样。
但是要注意的是,数据库不存在时会自动创建一个,但是自动创建的数据库类型是Sqlite2数据库,这个要注意下。

<?php
/*
注意细节:实例化对象时传入的是数据库的路径,要是数据库不存在的话会自动创建。
*/
class sqlite extends SQLite3{
    public $url;
    function __construct($url){
        $this->url=$url;
        $this->open($url);
    }
    function check_input($value){
        if (get_magic_quotes_gpc()) {
            $value = sqlite_escape_string($value);
        }
        return $value;
    }
    function create($table,$name,$type=null){
        $sql = &#39;CREATE TABLE &#39;.$table.&#39;(&#39;;
        if($type==null){
            $arrname = array_keys($name);
            $arrtype = array_values($name);
        }else{
            $arrname = explode("|", $name);
            $arrtype = explode("|", $type);
        }
        for($i=0;$i<count($arrname);$i++){
            if($i==count($arrname)-1){
                $sql = $sql.$arrname[$i]."   ".$arrtype[$i]."";
            }else{
                $sql = $sql.$arrname[$i]."   ".$arrtype[$i].",";
            }
             
        }
        $sql = $sql.&#39;);&#39;;
        $re = $this->query($sql);
        if($re){
            return true;
        }else{
            return fasle;
        }
    }
    function drop($table){
        $sql = &#39;DROP TABLE &#39;.$table.&#39;;&#39;;
        $re = $this->query($sql);
        if($re){
            return true;
        }else{
            return false;
        }
    }
    function insert($table,$name,$value=null){
        $sql = "INSERT INTO ".$table.&#39;(&#39;;
        if($value == null){
        $arrname = array_keys($name);
        $arrvalue = array_values($name);
        }else{
        $arrname = explode(&#39;|&#39;, $name);
        $arrvalue = explode(&#39;|&#39;, $value);
        }
        for($i=0;$i<count($arrname);$i++){
            if($i==count($arrname)-1){
                $sql = $sql.$arrname[$i];
            }else{
                $sql = $sql.$arrname[$i].",";
            }
        }
        $sql = $sql.")VALUES(";
        for($i=0;$i<count($arrvalue);$i++){
            if($i==count($arrvalue)-1){
                $sql = $sql."&#39;".$arrvalue[$i]."&#39;";
            }else{
                $sql = $sql."&#39;".$arrvalue[$i]."&#39;,";
            }
        }
        $sql .=");";
        if($debug){
            echo $sql;
        }
        $re = $this->query($sql);
        if($re){
            return true;
        }else{
            return false;
        }
    }
    function delete($table,$Conditionsname,$Conditionsvalue=null){
        if($Conditionsvalue!=null){
            $sql = "DELETE FROM ".$table." WHERE ".$Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
        }else{
            $sql = "DELETE FROM ".$table." WHERE ";
            $arrname = array_keys($Conditionsname);
            $arrvalue = array_values($Conditionsname);
            for($i=0;$i<count($arrname);$i++){
                if($i==count($arrname)-1){
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
                }else{
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;,";
                }
            }
            $sql.=&#39;;&#39;;
        }
        $re = $this->query($sql);
        if($re){
            return true;
        }else{
            return false;
        }
    }
    function select($table,$name,$Conditionsname,$Conditionsvalue=null){
        if($Conditionsvalue!=null){
            $sql = "SELECT ".$name." FROM ".$table." WHERE ".$Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
        }else{
            $sql = "SELECT ".$name." FROM ".$table." WHERE ";
            $arrname = array_keys($Conditionsname);
            $arrvalue = array_values($Conditionsname);
            for($i=0;$i<count($arrname);$i++){
                if($i==count($arrname)-1){
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
                }else{
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39; and ";
                }
            }
            $sql.=&#39;;&#39;;
        }
        $ret = $this->query($sql);
        $row = $ret->fetchArray(SQLITE3_ASSOC);
        return $row[$name];
    }
    function update($table,$name,$value,$Conditionsname,$Conditionsvalue=null){
        if($Conditionsvalue!=null){
            $sql = "UPDATE ".$table." SET ".$name."= &#39;".$value."&#39; WHERE ".
            $Conditionsname."=&#39;".$Conditionsvalue."&#39;;";
        }else{
            $sql = "UPDATE ".$table." SET ".$name."= &#39;".$value."&#39; WHERE ";
            $arrname = array_keys($Conditionsname);
            $arrvalue = array_values($Conditionsname);
            for($i=0;$i<count($arrname);$i++){
                if($i==count($arrname)-1){
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39;";
                }else{
                    $sql.=$arrname[$i].&#39;=&#39;."&#39;".$arrvalue[$i]."&#39; and ";
                }
            }
            $sql.=&#39;;&#39;;
        }
        $re = $this->query($sql);
        if($re){
            return true;
        }else{
            return false; 
        }
    }
    function group($table,$name){
        $sql = "SELECT ".$name." FROM ".$table.";";
        $return = array();
        $ret = $this->query($sql);
        while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
            array_push($return, $row[$name]);
        }
        return $return;
    }
    function fecthall($sql){
        $return = array();
        $ret = $this->query($sql);
        while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
            array_push($return, $row);
        }
        return $return;
    }
}

       

 以上就是封装的Sqlite3简易操作类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)