sqlite 类 php,php下操作sqlite的一个类

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 .= '
Sqlite Error: ' . $ei . ', ' . sqlite_error_string($ei);

if ($sql) {

$sql = '
sql:' . $sql;

}

exit('DataBase Error.
Message: ' . $message . $sql);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值