Php 操作sqlite3 文本数据库

sqlite 官方网站: http://www.sqlite.org/
php 官方网站文档: http://cn.php.net/manual/zh/book.sqlite3.php

php 操作sqlite3 文本数据库的类为: SQLite3

1.  声明一个sqlite3 文本数据库对像

      $db = new SQLite3('mysqlitedb.db');

2.  执行一个对文本数据库的查询

      SQLite3::query()

      $re = $db->query('select name from table');
      while ($row = $re->fetchArray()){
            var_dump($row);
            //print_r($row);
      }

      执行一个查询并返回一个结果

      SQLite3::querySingle()

      $db = new SQLite3('mysqlitedb.db');
      print_r ($db->querySingle('select * from table', true);

3.  对文本数据库执行: INSERT, UPDATE, DELETE 操作

      $db = new SQLite3('mysqlitedb.db');
      $db->exe   ('insert into table values ('name', 'teststring'));

4.  关闭文本数据库

      $db = new SQLite3('mysqlitedb.db');
      $db->close();

5.  如果对sqlite 数据做INSERT, UPDATE, DELETE 操作后,操作了多少行数据

      SQLite3::changes (void)
      $db->changes();

      $db = new SQLite3('mysqlitedb.db');
      $query = $db->exe     ('UPDATE counter SET views=0 WHERE page=”test”');
       if ($query) {
              echo “Number of rows modified: “, $db->changes();
       }

6.  如果操作sqlite 语句执行出错 ,取得出错信息及出错代码

      SQLite3::lastErrorMsg()
      SQLite3::lastErrorCode()

      $db = new SQLite3('mysqlitedb.db');
      $query = $db->exe     ('insert into table values (2, “a”, “b”,”c”)');
      if (!$query) {
            echo “error message: ”. $db->lastErrorMsg();
            echo “error code: “. $db->lastErrorCode();
      }else{
            return $query->changes();
      }

----------------------- Page 2-----------------------

7.  返回操作sqlite 时插入数据库中的最近插入行的ID

     SQLite3::lastInsertRowID (void)

8.  返回操作sqlite 时得到的关联或数字索引数组的结果行

      SQLite3Result::fetchArray (SQLITE3_ASSOC|SQLITE3_NUM|SQLITE3_BOTH)
      SQLITE3_ASSOC    返回一个以列名索引的数组
      SQLITE3_NUM       返回一个以数字为索引的数组
      SQLITE3_BOTH      返回一个以列名索引和数字索引的数组
      默认为: SQLITE3_ASSOC

      例一: 
      $db = new SQLite3('mysqlitedb.db');
      $sql = “select user_id, username from tbl_user”;
      $result = $db->query($sql);
      $row = array();
      $i = 0;

      while ($res = $result->fetchArray(SQLITE3_ASSOC)){
            if (!isset($res['user_id'])) continue;
            $row[$i]['user_id'] = $res['user_id'];
            $row[$i]['username'] = $res['username'];
            $i++;
      }
      print_r($row);

      例二:
      $db = new SQLite3('mysqlitedb.db');
      $re = $db->query('select * from tbl_user');
      while ($res = $re->fetchArray(SQLITE3_ASSOC)){
            if (!isset($res['name'])) continue;
            echo $res['name'] . “=>”. $res['password'] .”\n”;
      }

9.  关闭一个结果集

      SQLite3Result::finalize(void)

      $res->finalize()

10.  返回结果集的列数

        SQLite3Result::numColumns (void)

        $sql = “select * from tbl_name”;
        $res = $db->query($sql) or die (“Error in query: <span style='color:red;'> $sql </span>”);
        $num_columns = $res->numColumns();

原文地址:https://www.cnblogs.com/simadi/p/3124285.html