5

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...

4
  • 1
    Is there any error/message you received? Commented Apr 1, 2011 at 13:13
  • is that me or is there some stars after the $db = new PDO('sqlite:sampleDB.sqlite'); line ? if not me its gonna have parsing fail ...
    – yent
    Commented Apr 1, 2011 at 13:18
  • What if you try specifying full path to DB?
    – binaryLV
    Commented Apr 1, 2011 at 13:33
  • 1
    Is sqlite specified in list of available drivers? Try print_r2(PDO::getAvailableDrivers())
    – binaryLV
    Commented Apr 1, 2011 at 13:34

3 Answers 3

5

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.

1
  • 1
    How do you query database using this type of connection?
    – ST3
    Commented Oct 3, 2013 at 19:41
1

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);
    }

?>
4
  • i have tried to connect/create the database in the memory. it works. $db = new PDO("sqlite::memory");
    – ayathas
    Commented Apr 1, 2011 at 13:31
  • same code i have executed. i am getting error message on 'unable to open the database'
    – ayathas
    Commented Apr 1, 2011 at 13:32
  • Have you checked phpinfo() to make sure SQLLite support is enabled? Commented Apr 1, 2011 at 13:33
  • how to make sure the sQLite support enable?
    – ayathas
    Commented Apr 1, 2011 at 13:35
1

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'.

2
  • still i am getting the error msg. SQLSTATE[HY000] [14] unable to open database file
    – ayathas
    Commented Apr 1, 2011 at 13:44
  • this is how i changed my code. $dbh = new PDO('sqlite:myDir/sampleDB.sqlite');
    – ayathas
    Commented Apr 1, 2011 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.