CHAPTER 6 – SQLite – Setting Up Databases
Because SQLite doesn't require a daemon to function, setting up a database is in fact nothing more than creating a spe- cially formatted file. To create a new database, you simply try to open one; if the database does not exist, a new one will be created for you. That's the rea- son why the second parameter to the constructor can be used to specify the permissions for the created database. The example script we start with is the create.php script, which creates the database and all tables inside our database (see Table 6.6).
Table 6.6 Opening and Closing Databases Function Name Description sqlite_open(...) Connects the script to an SQLite database, or $sqlite = new SQLiteData- creates one if none exists yet. Parameters: base(...) · The path and file name (string) · Permissions in UNIX chmod style (octal number) · Error message (by-reference, string) sqlite_close(...) Disconnects the script from an SQLite database connection. The parameter is the SQLite descriptor. You can also create in-memory databases by using the special keyword ":memory:" as the first parameter to the SQLiteDatabase constructor. This allows for ultra-fast temporary SQL power. Do not forget to store your data somewhere else before ending a script; if you do not, the data you put into the database is gone. Here's an example: <?php $db = new SQLiteDatabase("./crm.db", 0666, &$error) or die("Failed: $error"); ... unset($db); ?>