How to INSERT values to SQLite3 file

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
anzu
Forum Newbie
Posts: 2
Joined: Thu Nov 03, 2011 9:53 pm

How to INSERT values to SQLite3 file

Post by anzu »

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:

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 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
anzu
Forum Newbie
Posts: 2
Joined: Thu Nov 03, 2011 9:53 pm

Re: How to INSERT values to SQLite3 file

Post by anzu »

Okay, I figured it out so I'm going to solve my own problem here (lol). Turns out that the entire folder containing the DB file had to be writable, not just the DB file itself. Derp...anyways hope this helps someone with a similar problem =)
Post Reply