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 ... Commented Apr 1, 2011 at 13:18
  • What if you try specifying full path to DB? Commented Apr 1, 2011 at 13:33
  • 1
    Is sqlite specified in list of available drivers? Try print_r2(PDO::getAvailableDrivers()) 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.

Sign up to request clarification or add additional context in comments.

1 Comment

How do you query database using this type of connection?
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 Comments

i have tried to connect/create the database in the memory. it works. $db = new PDO("sqlite::memory");
same code i have executed. i am getting error message on 'unable to open the database'
Have you checked phpinfo() to make sure SQLLite support is enabled?
how to make sure the sQLite support enable?
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 Comments

still i am getting the error msg. SQLSTATE[HY000] [14] unable to open database file
this is how i changed my code. $dbh = new PDO('sqlite:myDir/sampleDB.sqlite');

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.