How to INSERT values to SQLite3 file
Posted: Thu Nov 03, 2011 10:25 pm
Hello, I am new to PHP and I've been reading and searching all over the web trying to figure this out but can't get it to work no matter which way I try it.
I need to simply insert a new row (and also be able to delete rows) in an SQLite3 DB file. I'm able to successfully list contents of the table, but when I try to INSERT new rows into the table it doesn't seem to work. After trying several methods, this is the code I've come up with:
I run the code and see the echo messages, no errors or anything. But when I check the db file there is no changes to it. I have double checked file permissions, even set permissions for the db file to 777 (world-writable) but no luck. My database is very simple, its just one table called BANS, with 3 columns (GUID, REASON, DATE). Could someone please give me a hint as to what I'm doing wrong here? This is my first experience with manipulating DBs in PHP, so I'm really a noob.
Any help is much appreciated! =)
I'm running this on:
Debian 6
Apache 2
PHP 5.3.3-7+squeeze3
SQLite Library 3.7.3
I need to simply insert a new row (and also be able to delete rows) in an SQLite3 DB file. I'm able to successfully list contents of the table, but when I try to INSERT new rows into the table it doesn't seem to work. After trying several methods, this is the code I've come up with:
Code: Select all
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
try {
/*** Open db connection or file ***/
$dbhandle = new PDO('sqlite:globalbans.db');
echo 'Connected to database<br />';
/*** Add new ban for setGUID ***/
$count = $dbhandle->exec("INSERT INTO BANS VALUES ('aabbccddeeff11223344556677889900', 'TestReason', '11052011')");
/*** Echo number of rows affected ***/
echo 'Successfully added ' . $count . ' bans to the list';
/*** Close the db ***/
$dbhandle = null;
echo 'Closed database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>I'm running this on:
Debian 6
Apache 2
PHP 5.3.3-7+squeeze3
SQLite Library 3.7.3