PHP:SQLite3数据库操作代码

SQLite3数据库功能很强大,未来中小企业网站(其实很多大公司、智能手机、平板电脑都用它)都使用SQLite数据库来存储了。

<?php
$db_path = 'sqlite3_db_php';
//public SQLite3::__construct ( string $filename [, int $flags [, string $encryption_key ]] )
//Returns an SQLite3 object on success.
$db = new SQLite3($db_path);
if (!!$db) {
        //public bool SQLite3::exec ( string $query )
        //$query : The SQL query to execute (typically an INSERT, UPDATE, or DELETE query).
        //Returns TRUE if the query succeeded, FALSE on failure.
        //下面创建一个表格
        $db->exec('CREATE TABLE user (id integer primary key, name varchar(32), psw varchar(32))');
        //插入记录
        $name = rand(1001,9999);
        $psw = md5($name);
        $db->exec("INSERT INTO user VALUES (NULL, '{$name}', '{$psw}')");
        //public int SQLite3::lastInsertRowID ( void )
        echo "n成功插入记录:",$db->lastInsertRowID(),"n";
        //public SQLite3Result SQLite3::query ( string $query )
        //Returns an SQLite3Result object if the query returns results. Otherwise, returns TRUE if the query succeeded, FALSE on failure.
        //查询记录
        $result = $db->query('SELECT * FROM user ORDER BY id DESC LIMIT 0,10');
        //public int SQLite3Result::numColumns ( void )
        //public string SQLite3Result::columnName ( int $column_number )
        $cols = $result->numColumns();
        for($i=0; $i<$cols; $i++){
                echo $result->columnName($i),"t";
        }
        //public array SQLite3Result::fetchArray ([ int $mode = SQLITE3_BOTH ] )
        //Fetches a result row as an associative or numerically indexed array or both. By default, fetches as both.
        //$mode : SQLITE3_ASSOC, SQLITE3_NUM, or SQLITE3_BOTH.
        //Returns a result row as an associatively or numerically indexed array or both.
        while($row = $result->fetchArray(SQLITE3_ASSOC)){
                echo "n{$row['id']}t{$row['name']}t{$row['psw']}";
        }
        //重置结果集指针到第一条之前
        //public bool SQLite3Result::reset ( void )
        //Resets the result set back to the first row.
        //Returns TRUE if the result set is successfully reset back to the first row, FALSE on failure.
        $result->reset();
        //public bool SQLite3Result::finalize ( void )
        //释放结果集
        $result->finalize();
        //public bool SQLite3::close ( void )
        //关闭数据库
        $db->close();

        //public SQLite3Stmt SQLite3::prepare ( string $query )
        //Returns an SQLite3Stmt object on success 或者在失败时返回 FALSE.
        //预处理查询
        $stmt = $db->prepare('SELECT bar FROM foo WHERE id=:id');
        //public bool SQLite3Stmt::bindValue ( string $sql_param , mixed $value [, int $type ] )
        //Returns TRUE if the value is bound to the statement variable, FALSE on failure.
        //$type:
                //SQLITE3_INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
                //SQLITE3_FLOAT: The value is a floating point value, stored as an 8-byte IEEE floating point number.
                //SQLITE3_TEXT: The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16-LE).
                //SQLITE3_BLOB: The value is a blob of data, stored exactly as it was input.
                //SQLITE3_NULL: The value is a NULL value
        //绑定值
        $stmt->bindValue(':id', 1, SQLITE3_INTEGER);
        //public SQLite3Result SQLite3Stmt::execute ( void )
        //Returns an SQLite3Result object on successful execution of the prepared statement, FALSE on failure.
        //执行预处理
        $result = $stmt->execute();
        //public bool SQLite3Stmt::clear ( void )
        //Returns TRUE on successful clearing of bound parameters, FALSE on failure.
        //清除所有绑定参数
        //public int SQLite3Stmt::paramCount ( void )
        //Returns the number of parameters within the prepared statement.
        //
        //public bool SQLite3Stmt::bindParam ( string $sql_param , mixed &$param [, int $type ] )
        //Returns TRUE if the parameter is bound to the statement variable, FALSE on failure.
        //
}
?>

相关文章

Office 2003 SP2 简体中文完整版下载与25位密钥PHP:ODBC处理EXCEL电子表格数据库实例
PHP下载文件代码如何删除Word页眉横线
MySQL创建数据库:CREATE DATABASE语法读书郎学生平板电脑G50,G12,G11详细介绍
爱学宝平板学习机A8,A33,A36,A511,AX730,AX750详细介绍2013年平板电脑品牌排行榜
AJAX+PHP局部刷新代码SQLite的数据类型
Javascript读取与设置cookie代码用汇编语言修改IE浏览器首页
PHP:SQLite3数据库操作代码apache2.4.6 httpd.exe命令
MySQL5存储过程视频教程,PHP操作MySQL存储过程示例MySQL数据库定义语句:ALTER DATABASE语法
使用PHP的ODBC函数库操作ACCESS数据库我要读书——农村儿童的心声!
PHP+SQLite数据库操作教程与实例ROWIDs and the INTEGER PRIMARY KEY
版权所有 © 中山市飞娥软件工作室 证书:粤ICP备09170368号