Blog Links

SQLite V3 with PHP – sample script

sqliteSQLite [http://www.sqlite.org] is a very handy tool, essentially it creates a database in a file. SQLite V3 should be installed automatically with PHP5.

I find SQLite very handy when I want to make standalone code that uses a database, and because it doesn’t require an external application(such as Mysql) it makes it very portable. I hunted around the net but couldn’t find a good starting point with PHP, so did a little research and knocked one up.

This sample program shows the basics of creating a database, a table, inserting records, and then displaying them. (note: The syntax for SQLite V2 is very different from V3 so check to make sure which version you are running)

// SQLite V3 - Test Program
// A simple test program to :-
// 1) Create a database file
// 2) Create a table
// 3) Create an index
// 4) Start a transaction
// 5) Insert some data
// 6) Complete the transaction
// 7) Display the records sorted by the index

$dbname = "testDB";
$tablename = "testTABLE";

if ($db = new PDO("sqlite:$dbname")) {
	$query = @$db->query("SELECT * FROM $tablename");
	if ($query === false) {
		$db->query("CREATE TABLE $tablename (
		           id INT NOT NULL ,
		           rand INT NOT NULL ,
		           PRIMARY KEY ( id ));");
		echo "Created $tablename \n";

		$db->query("CREATE index testINDEX on $tablename ( rand)");
		echo "Created index \n";

	} else {
                echo "$tablename and $dbname Already exists\n";
	}

	$db->query("delete from $tablename");
	echo "All records deleted\n";

	$db->beginTransaction();
	for ($n=1;$n<=10;$n++) {
		$random_num = rand(0,100000);
		$sql="INSERT INTO $tablename ( id , rand )	VALUES ($n, $random_num );";
		$db->query($sql);
	}
	$db->commit();
} else {
	die($err);
}

echo "Test Data Insert Complete\n";
$r = $db->query("select * from $tablename order by rand");
while ($row = $r->fetch(SQLITE_ASSOC)) {
	echo $row["id"]."\t".$row["rand"]."\n";
}

This program stores all its data in a file called “testDB”.
There’s also a handy command line program to access your database file, so after running the program

sqlite3 testDB

allows you to manage the database. (similar to the mysql command line tool).

If this has been useful to you, and would like to buy me a coffee (via paypal) please click here.

  • Share/Save/Bookmark

An easy way to use GNU Screen over SSH

GNU Screen is a free terminal multiplexer developed by the GNU Project since at least 1995. It allows a user to access multiple separate terminal sessions inside a single terminal window or remote terminal session. It is useful for dealing with multiple programs from the command line, and for separating programs from the shell that [...]

Linux – disk usage (du) human readable AND sorted by size

This is quick tip to fix a problem that has always bugged me – When showing disk usage in a human readable form (KB, MB, GB) for each subdirectory using “du -sh *”, how can you properly sort it into size order.
If you just want the solution here it it…

pre.command {
width:80%;
background-color:#FFF5F5;
border:1px dashed #BB8888;
margin:10px;
padding:10px;
}

alias duf=’du [...]

XPaths with PHP by example

Learn by example how to scrape web pages using PHP and Xpath.

Earth Info – Some links about Earth

If you found this page you may have been searching for something about The Earth, Im sorry to dissapoint you, but here are a few decent links that may get you started on your research.

http://en.wikipedia.org/wiki/Earth – Aways a good starting point for your research
http://earthobservatory.nasa.gov/ – Nasa’s Earth observatory
http://www.earth.columbia.edu/ – The Earth Institue at Columbia University
http://science.nationalgeographic.com/science/earth.html [...]