TA的每日心情 | 开心 2013-10-31 08:31 |
---|
签到天数: 16 天 连续签到: 1 天 [LV.4]偶尔看看III
|
楼主 |
发表于 2014-4-26 17:48:36
|
显示全部楼层
想使用php操作sqlite3 数据库,还是没有成功。
下面的代码提示缺少类,应该是要包含头文件,不知道是什么头文件!!- <?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.
- //
- }
- ?>
复制代码 |
|