PHP操作sqlite类

<?php



    class Sqlite {

        var $link;

        var $querynum = 0;



        /*连接Sqlite数据库,参数:dbname->数据库名字*/

        function Open($dbname) {

            if(!($this->link = @sqlite_open($dbname))) {

                $this->halt('Can not Open to Sqlite');

            }

        }



        /*执行sql语句,返回对应的结果标识*/

        function Query($sql) {

            $this->querynum++;

            if($query = @sqlite_query($this->link, $sql)) {

                return $query;

            } else {

                $this->halt('Sqlite Query Error', $sql);

            }

        }



        /*执行Insert Into语句,并返回最后的insert操作所产生的自动增长的id*/

        function Insert($table, $iarr) {

            $value = $this->InsertSql($iarr);

            $this->Query('INSERT INTO "' . $table . '" ' . $value);

            return sqlite_last_insert_rowid($this->link);

        }



        /*执行Update语句,并返回最后的update操作所影响的行数*/

        function Update($table, $uarr, $condition = '') {

            $value = $this->UpdateSql($uarr);

            if ($condition) {

                $condition = ' WHERE ' . $condition;

            }

            $this->Query('UPDATE "' . $table . '"' . ' SET ' . $value . $condition);

            return sqlite_changes($this->link);

        }



        /*执行Delete语句,并返回最后的Delete操作所影响的行数*/

        function Delete($table, $condition = '') {

            if ($condition) {

                $condition = ' WHERE ' . $condition;

            }

            $this->Query('DELETE "' . $table . '"' . $condition);

            return sqlite_changes($this->link);

        }



        /*将字符转为可以安全保存的sqlite值,比如a'a转为a''a*/

        /*

        function EnCode($str) {

            if (strpos($str, "\0") === false) {

                if (strpos($str, '\'') === false) {

                    return $str;

                } else {

                    return str_replace('\'', '\'\'', $str);

                }

            } else {

                $str = str_replace("\0", '', $str);

                if (strpos($str, '\'') === false) {

                return $str;

                } else {

                    return str_replace('\'', '\'\'', $str);

                }

            }

        }

        */

        function EnCode($str) {

            return sqlite_escape_string($str);

        }



        /*将可以安全保存的sqlite值转为正常的值,比如a''a转为a'a*/

        function DeCode($str) {

            if(strpos($str, '\'\'') === false) {

                return $str;

            } else {

                return str_replace('\'\'', '\'', $str);

            }

        }



        /*将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回("id", "name") VALUES (1, 'name')*/

        function InsertSql($iarr) {

            if (is_array($iarr)) {

                $fstr = '';

                $vstr = '';

                foreach ($iarr as $key => $val) {

                    $fstr .= '"' . $key . '", ';

                    $vstr .= '\'' . $val . '\', ';

                }

                if ($fstr) {

                    $fstr = '(' . substr($fstr, 0, -2) . ')';

                    $vstr = '(' . substr($vstr, 0, -2) . ')';

                    return $fstr . ' VALUES ' . $vstr;

                } else {

                    return '';

                }

            } else {

                return '';

            }

        }



        /*将对应的列和值生成对应的insert语句,如:array('id' => 1, 'name' => 'name')返回"id" = 1, "name" = 'name'*/

        function UpdateSql($uarr) {

            if (is_array($uarr)) {

                $ustr = '';

                foreach ($uarr as $key => $val) {

                    $ustr .= '"' . $key . '" = \'' . $val . '\', ';

                }

                if ($ustr) {

                    return substr($ustr, 0, -2);

                } else {

                    return '';

                }

            } else {

                return '';

            }

        }



        /*返回对应的查询标识的结果的一行*/

        function GetRow($query, $result_type = SQLITE_ASSOC) {

            return sqlite_fetch_array($query, $result_type);

        }



        /*清空查询结果所占用的内存资源*/

        function Clear($query) {

            $query = null;

            return true;

        }



/*关闭数据库*/

        function Close() {

            return sqlite_close($this->link);

        }



        function halt($message = '', $sql = '') {

            $ei = sqlite_last_error($this->link);

            $message .= '<br />Sqlite Error: ' . $ei . ', ' . sqlite_error_string($ei);

            if ($sql) {

                $sql = '<br />sql:' . $sql;

            }

            exit('DataBase Error.<br />Message: ' . $message . $sql);

        }

    }

?>

 

你可能感兴趣的:(sqlite)