Tuesday, 6 January 2015

Accessing a SQLite database table from PHP 5.4 upwards

Text in Courier is code to be typed literally
Text in Arial should be substituted for your own values 

<?php

try {
   $file = 'path/to/sqlfile.sl3';  // relative to the directory the php is in 
   $db = new PDO('sqlite:' . $file);  // new php db object, *not* a new db
 } 
catch(PDOException $pe) {
   echo $pe->getMessage();
   exit(0);
 }

try {
   $result = $db->query('SELECT field1,field2,field3,etc FROM table');
   echo '<table>' . "\r\n";
   echo "<tr><th>field1 name</th><th>field2 name</th><th>field3 name</th></tr>\n";
   foreach ( $result as $row ) {
      echo "<tr><td>" . $row['field1'] . "</td>" . 
           "<td>" . $row['field2'] . "</td>" .
           "<td>" . $row['field3'] . "</td>" .
           "</tr>\r\n";
   }
   echo '</table>' . "\r\n";
 } 
catch(PDOException $pe) {
   echo $pe->getMessage();
   exit(0);
 }
?>

How to get SQLite3 working in PHP 5.4, 5.5 and up - on Windows

1) Check to see if PHP believes you have SQLite3 installed and working

Create this PHP script (I created it as sqlSandbox.php in the top-level of my web-server's web root directory)

<?php
// Give yourself all the debugging info you can
error_reporting(E_ALL | E_STRICT);

ini_set('display_errors', TRUE);

// What version of PHP is running?
echo "phpversion " . phpversion();

echo "<br>";

// Everything and the Kitchen sink
phpinfo();
?>

Things to check on this page:
A) Top section
Loaded Configuration FileM:\php-5.5.10\php.ini
This shows you which php.ini file is the one to alter.
B) In the PDO section
You should see
PDO supportenabled
PDO driverssqlite

C) There should be a pdo_sqlite section.
PDO Driver for SQLite 3.xenabled
SQLite Library3.7.7.1

D) There should be a sqlite3 section
SQLite3 supportenabled
SQLite3 module version0.7-dev
SQLite Library3.7.7.1

DirectiveLocal ValueMaster Value
sqlite3.extension_dirM:\sqlite3M:\sqlite3


2) Note that any function in the PHP manual beginning "sqlite_"  refers to sqlite2 and below, and that only references beginning with SQLite3 work with SQLite3.

3) Make sure you have a php.ini file in your php directory
(I created mine by copying php-production.ini to php.ini)

4) Make sure PHP can see the PHP Windows extensions
In php.ini
Uncomment the last line of this section, (by removing the ; at the start of the line) and add the ext sub-directory

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir =

So the final line becomes:

extension_dir = ext

5) Enable the PDO Windows extension for SQLite
In php.ini
Uncomment this line (by removing the ; at the start of the line)
to get
extension=php_pdo_sqlite.dll

6) Enable the SQLite3 Windows extension
In php.ini
Uncomment this line (by removing the ; at the start of the line)
to get
extension=php_sqlite3.dll

7) Tell PHP where SQLite is.
Uncomment this line (by removing the ; at the start of the line) and put in the relevant directory
[sqlite3]
sqlite3.extension_dir = M:\sqlite3

Tuesday, 4 November 2014

SQLite3 Quirks

CHAR & VARCHAR do not exist

VARCHAR gets treated as TEXT

Primary Keys get some additional automagic functionality when they are INTEGER
 1) They autoincrement
plus Something Weird?




Monday, 3 November 2014

Reminder of the b******g obvious in SQL



1) Foreign Key : a column (or a combination of columns) whose values match a Primary Key in a different table.

i.e. There has to be a Primary Key in this table which matches the foreign key in that table (or vice versa)

Someone else's looser definition:
A foreign key is a column (or columns) that references a column (most often the primary key) of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.

i.e attributes in table A must hold a value listed in table B


2) All values in a Primary Key have to be unique








Friday, 31 October 2014

The Briefest-possible Beginner's Guide to Mercurial Distributed Version Control System

Mercurial commands

hg log 
show list of commits

hg diff
show lines which differ between tip and working copy

hg diff -c <commit number>
show diffs caused by commit number - commit number shown in log

hg update <commit number>
set working copy to state of given commit

hg update tip
update to latest commit

hg tag <tag-name> 
mark last commit with a tag - When you have a version you're happy with, tag it

hg update <tag-name>
retrieve tagged commit to working copy
i.e. "update your working copy to match a given previous version"
only possible if there are no uncommitted changes   

hg clone <working copy path>
places a clone of the the working copy in the current directory

i.e.
in Mercurial 



 

How to Switch off a second monitor

I wanted to switch off my second monitor on my Windows 7 Home Premium PC, so that I could connect in to my dev PC, using VNC on a Chromebook.

If I keep an extended desktop on the dev PC, then there is a lot of screen to scroll across on the Chromebook.

I wanted to preserve the settings for the second monitor, so that I did not have to spend a lot of time, re-configuring the second monitor to be just the way I liked it when I started using it again.

The answer turned out to be really simple.

How to turn off your second monitor in Windows 7, without losing the settings for it :

Windows key  + p
Change from "Extend" to "Duplicate" in the onscreen dialog.
Power off the second monitor.

To returnback to the original settings, simply
Windows key + p
Change from "Duplicate" to "Extend"
And remember to switch the monitor back on!  :-)