Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: SQLite 関数 (PDO_SQLITE) - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

PDO_SQLITE DSN" width="11" height="7"/> <sqlite_valid
Last updated: Thu, 31 May 2007

view this page in

CLVII. SQLite 関数 (PDO_SQLITE)

導入

PDO_SQLITE は、PHP から SQLite 2 や SQLite 3 データベースへのアクセスを可能にするための PHP Data Objects (PDO) インターフェース を実装したドライバです。

PHP 5.1 では、SQLite 拡張モジュールも SQLite 2 データベースに対するドライバを提供しています。 理論的には PDO_SQLITE ドライバの一部ではなく動作も同様なので、 平行してドキュメント化されています。PDO 用 SQLite 2 ドライバは主に レガシーな SQLite 2 データベースファイルをより高速でより効果的な SQLite 3 ドライバを使用するアプリケーションへ 簡単にインポートするために提供されています。 結果として、SQLite 2 ドライバは SQLite 3 ドライバよりも機能豊富ではありません。

目次

PDO_SQLITE DSN — SQLite データベースに接続する
PDO->sqliteCreateAggregate() — SQL 文で使用する集約ユーザ定義関数 (UDF) を登録する
PDO->sqliteCreateFunction() — SQL 文で使用するユーザ定義関数 (UDF) を登録する


PDO_SQLITE DSN" width="11" height="7"/> <sqlite_valid
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
SQLite 関数 (PDO_SQLITE)
gopal at gopalarathnam dot com
27-Mar-2007 07:36
If you get an error reporting "invalid resource" when trying to query the database table and looping through it, the version of the SQLite extension compiled in to PHP might be incompatible with the version that had created the database (like SQLite 2.x vs 3.x).

The database open itself might be successful, failing only when querying.

$dbh = new PDO('sqlite:/tmp/foo.db'); // success
foreach ($dbh->query('SELECT * FROM bar') as $row) // prints invalid resource
    // ...
gmac63 at charter dot net
10-Aug-2006 11:38
Issue:
Error: SQLSTATE[HY000]: General error: 1 unsupported file format

Resolution:
To solve this (and/or many issues) involving this type of error, I assumed the error to be generated from php. Well, it was to an extent. The sqlite pdo code offered the solution:

I researched the error by grep'ing the php source code and found the error string to come from php-5.1.4/ext/pdo_sqlite/sqlite/src/prepare.c, lines 265:278 :

/*
 ** file_format==1    Version 3.0.0.
 ** file_format==2    Version 3.1.3.
 ** file_format==3    Version 3.1.4.
 **
 ** Version 3.0 can only use files with file_format==1. Version 3.1.3
 ** can read and write files with file_format==1 or file_format==2.
 ** Version 3.1.4 can read and write file formats 1, 2 and 3.
 */
 if( meta[1]>3 ){
   sqlite3BtreeCloseCursor(curMain);
   sqlite3SetString(pzErrMsg, "unsupported file format", (char*)0);
   return SQLITE_ERROR;
 }

This is interesting as I am running SQLite version 3.3.5 which the databases were created in. I see that the SQLite PDO source in the php source is :
      # cat ext/pdo_sqlite/sqlite/VERSION
       3.2.8

My solution was then to find a version of sqlite that was =< 3.1.4. I found source for 3.1.3, compiled, recreated my database using that version (NOTE: the databases are unsupported between 3.1.x and 3.2.x versions of SQLite). Once I did this it worked.

Also as a side note, to get SQLite compiled as a PDO, I had to:

1) configure with
...
--enable-pdo=shared \
--with-sqlite=shared \
--with-pdo-sqlite=shared
--with-zlib
... \
'make && make install' if configure is successful.

2) Make sure the pdo libs were copied/installed to the correct directory. On my installation it was /usr/local/include/php/ext/pdo/

3) Make these changes in my php.ini:
  - change ' extension_dir = "./" ' to ' extension_dir="/usr/local/include/php/ext/pdo/" '
  - add/edit in this order:
      extension=pdo.so
      extension=pdo_sqlite.so
      extension=sqlite.so

4) test php with : 'php -m' at the command line and solve any issues from there. Mostly php.ini config issues. Also restart the http service!
maximkh [at] yahoo [dot] com
05-Aug-2006 05:11
In reply to Duffalo:

The current PHP version will happily work with SQLite 3.3.6, but you need to re-compile PHP using the latest SQLite library.

On a different note, it seems that the developers got a little lazy and decided not to add any support for integer, real, or blob data types. When using PDO's bindParam or bindValue methods, the driver only uses either sqlite3_bind_null, or sqlite3_bind_text. That means that if you think you're storing ints, even with PDO::PARAM_INT specified, it is still stored as string, taking up a lot more space than it needs to and effectively eliminating all data-type support for SQLite (you are only left with nulls or text).

In my opinion, this is a rather lousy implementation. I've submitted a feature/change request to the bug tracking system, so I guess we can wait and see if full data type support is added in the future. What I don't understand is why this was done in the first place. SQLite's API could not be any easier to use, simply check the type of the variable passed, and if it's an int use sqlite3_bind_int, if it's a float use sqlite3_bind_double. Why the forced conversion into strings?
Duffalo
07-Jul-2006 08:41
Note that as of the date of this post, PDO_SQLITE will not interact with database files created with the current version of the SQLite console application, sqlite-3.3.6.

It is currently necessary to obtain version 3.2.8, available from http://www.sqlite.org/ but only by entering the URI manually, as there is no link. Go to http://www.sqlite.org/download.html and find the URI of the version you're looking for, then make the appropriate version number substitution.
aidan at php dot net
01-Oct-2005 06:36
If you receive an error while trying to write to a sqlite database (update, delete, drop):

Warning: PDO::query() [function.query]: SQLSTATE[HY000]: General error: 1 unable to open database

The folder that houses the database file must be writeable.

PDO_SQLITE DSN" width="11" height="7"/> <sqlite_valid
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites