Zend Framework: Getting started using SQLite Zend Framework: Getting started using SQLite sqlite sqlite

Zend Framework: Getting started using SQLite


@tuinstoel is correct, attaching to an SQLite database implicitly creates it if it does not exist.

SQLite also supports a command-line client that is more or less like MySQL's command shell, allowing you to issue ad hoc commands or run SQL scripts. See documentation here: http://www.sqlite.org/sqlite.html

Of course you need to change the Zend_Db adapter in your ZF application. ZF supports only an adapter to the PDO SQLite extension. SQLite doesn't support user/password credentials. Also since SQLite is an embedded database instead of client/server, the "host" parameter is meaningless.

$db = Zend_Db::factory("pdo_sqlite", array("dbname"=>"/path/to/mydatabase.db"));

One more caveat: when you get query results in associative-array format, some versions of SQLite insist on using "tablename.columnname" as the keys in the array, whereas other brands of database return keys as simply "columnname". There's an outstanding bug in ZF about this, to try to compensate and make SQLite behave consistently with the other adapters, but the bug is unresolved.


If you make a connection to a not existing database, a database is created on the fly. (You can turn this behavour off)


This is now covered in the Zend Framework quickstart tutorial (version 1.9.5 as of this writing). Just make a new project (with zf command line tool. look here for a great tutorial on setting it up), add these lines to your config.ini file and you're good to go:

; application/configs/application.ini[production]resources.db.adapter       = "PDO_SQLITE"resources.db.params.dbname = APPLICATION_PATH "/../data/db/databaseName.db"

Now when you ask for your default database adapter, it will use this one. I would also recommend downloading the quickstart tutorial source code and making use of the load.sqlite.php script. You can create a schema and data file and load the database with these tables/columns/values. It's very helpful! Just check out the tutorial. It's all in there.


This answer was moved out of the question into a CW answer to disavow ownership over the content.