ayathas
ayathas

Reputation: 51

how to connect a sqlite database using PHP?

i have tried lot to connect database using PHP PDO. i have got lot of samples, i am not sure what was problem.

below is my code

<?php
    try
    {
        // $db = new PDO('sqlite:sampleDB.db3');
        // $db = new SQLiteDatabase('sampleDB.sqlite', 0666, $error);  
        $db = new PDO('sqlite:sampleDB.sqlite');
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
?>

i have tried lot of way to open a connection. please tell me the correct way...

Upvotes: 5

Views: 16563

Answers (3)

Michał Niedźwiedzki
Michał Niedźwiedzki

Reputation: 12939

The directory the sql file is located should be server writable. Try creating separate directory just for SQLite and give it appropriate access. In Unix you can do it so by running chmod 777 dirname. Also, amend your DSN to 'sqlite:dirname/sampleDB.sqlite'.

Upvotes: 1

Black Box Operations
Black Box Operations

Reputation: 257

First, you'll want to make sure you've got PHP configured to connect to SQLite - use phpinfo() to check and be sure that you have SQLite support enabled.

Next, you'll want to use the proper syntax when attempting to connect and query against a SQLite database. As per PHP Manual for sqllite e.g.

<?php

    if ($db = sqlite_open('sampleDB', 0666, $sqliteerror) ) { 
        $result = sqlite_query($db, 'select bar from foo');
        var_dump(sqlite_fetch_array($result) ); 
    } else {
        die($sqliteerror);
    }

?>

Upvotes: 1

petermolnar
petermolnar

Reputation: 1626

I gave up with PDO driver, and used sqlite3 module instead for same reasons.

With sqlite3 module:

class DB extends SQLite3
{
        function __construct( $file )
        {
            $this->open( $file );
        }
}

$db = new DB( 'sampleDB.sqlite' );

I know it's not a solution for your problem, but if nothing else works, this could be useful.

Upvotes: 5

Related Questions