PHP sqlite

Sqlite is a great piece of open source software. Many larger organizations are making use of it, such as: Adobe, Apple, Firefox, and various others. It is a great zero configuration transportable database format for your application.

Here is an example of using sqlite in your php application. You need to have sqlite installed and configured with your php installation. You also will need a folder that your php app has write permission for. The example I am showing today uses php data objects ( PDO ) extension, so make sure that is installed/configured as well.

The basics are pretty simple, first create a pdo object referencing the sqlite file.

$sql_lite = new PDO('sqlite:folder/db.sqlite');

You can then use that object to run queries against the database. You will of course want to create some initial tables to store your data in. Then you can run queries against this table just like business as usual.

	$sql_lite->query("
CREATE TABLE user (
	username varchar(100) NOT NULL
	,password varchar(200) NOT NULL
	,UNIQUE (username)
);
");

	$sql_lite->query("insert into user (username,password)
		values('mike','". md5('boo') ."');");
	$sql_lite->query("insert into user (username,password)
		values('test','". md5('test') ."');");

	$users = $sql_lite->query('select * from user');
	foreach($users as $u) {
		echo $u['username'] .'
'; }

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up