How to install sqlite or sqlite 3 database with xampp server and how to use sqlite or sqlite 3 database using php scripts or programs...
4 Answers
Creating the sqlite Database
You have two possibilities to achieve that:
Writing your DDL-statements (CREATE TABLE...) in a .sql file and execute it using the sqlite command line (assuming your CREATE TABLE statements are in a file called tabledef.sql):
cat tabledef.sql | sqlite3 yourdbname.db- Use PHP to execute the DDL-statements on a connected database (see "Executing statements").
Connecting to a sqlite Database
You should definitely use PDO to do that:
$dbh = new PDO('sqlite:/path/to/your/database.db', '', '', array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));
If the database does not exist, then it is created. But you need write-access to the directory that contains the database. If you permit write-access only to the database file, sqlite fails because it also needs to create a lock file in the same directory.
Executing statements
Now that you have a connection you can do your stuff with the database, for example execute some DDL-statements:
$dbh->exec("CREATE TABLE IF NOT EXISTS mytable (
mypk INTEGER PRIMARY KEY AUTOINCREMENT,
myvalue TEXT);");
Or if you need to dynamically generate SQL statements, use prepared statements:
$statement = $dbh->prepare("INSERT INTO mytable (myvalue) VALUES(?)");
$statement->execute(array("hello"));
$statement->execute(array("world"));
This is only a small overview, for further information you should check out the PDO manual and the sqlite Documentation.
1 Comment
Well to see if it's installed, check phpinfo() and see if the drivers for sqlite are installed (search the list for "sqlite"), it will mention sqlite and and sqlite 3 seperately. I think they both come preinstalled with XAMPP so it should just work. Then check the PHP manual for sqlite functions, or use the PDO wrappers. Google is your friend.
1 Comment
You can use a database wrapper, that can make things much easier.
try this one, i made it :
<?php
Class Sqlite3_Wrapper
{
private $sFileName, $oCon=null, $iDebug, $oResult;
private $sDefaultFile = 'default.db';
public function __construct($sFileName, $iDebug=false)
{
$this->setFileName($sFileName);
}
private function showMessage($sMsg)
{
if($this->iDebug != false)
{
$sMessage = (php_sapi_name() == 'cli')?$sMsg."\n":$sMsg.'<br />' ;
echo $sMessage;
}
}
public function setFileName($sFileName)
{
if(is_string($sFileName) && strlen($sFileName)>0)
{
$this->sFileName = $sFileName;
}
else
{
$this->sFileName = $this->sDefaultFile;
}
}
public function openDB($sFileName=false)
{
if($sFileName!==false)
{
$this->setFileName($sFileName);
}
try
{
$this->oCon = new SQLite3($this->sFileName);
}catch(Exception $oEx)
{
$this->showMessage($ex->getMessage());
}
}
public function closeDB()
{
if($this->oCon!=null)
{
$this->oCon->close();
$this->oCon = null;
}
}
public function executeCUDQuery($sQuery)
{
if($this->oCon==null)
{
$this->showMessage('Not connected to database');
}
else
{
try
{
$this->oCon->exec($sQuery);
}catch(Exception $ex)
{
$this->showMessage($ex->getMessage());
}
}
}
public function executeQuery($sQuery)
{
$this->oResult = null;
if($this->oCon==null)
{
$this->showMessage('Not connected to database');
}
else{
try
{
$this->oResult = $this->oCon->query($sQuery);
}catch(Exception $oEx)
{
$this->showMessage($oEx->getMessage());
}
}
return $this->oResult;
}
public function fetch($iMode=SQLITE3_NUM)
{
$aData = array();
try
{
$aData = $this->oResult->fetchArray($iMode);
}
catch(Exception $oEx)
{
$this->showMessage($oEx->getMessage());
}
return $aData;
}
}
?>
or get the files here :
http://vaan3713.github.com/databaseswrappers/
wish it will help.