Welcome Guest, Not a member yet? Register   Sign In
Problem connecting to sqlite database
#1

[eluser]grumble[/eluser]
Hi good CI people,

I hate for my first post to be a cry for help, rather than something more positive, but needs must... I've just started using CI and am trying to connect to my SQLite database. Here is my setup.

database.php:
Code:
...
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "";
$db['default']['password'] = "";
$db['default']['database'] = "<path to sqlite db>/test.db";
$db['default']['dbdriver'] = "sqlite";
...
(of course, replace "&lt;path to sqlite db&gt;" with my actual path)
../application/controllers/test_controller.php:
Code:
&lt;?php
class Test_controller extends Controller {

    function index()
    {
        echo "test 1";
        $this->load->database();
        echo "test 2";
    }
}
?&gt;

I can see "test 1" on my page, but not "test 2". I don't get any error messages or anything. "test.db" is readable by everybody. Any suggestions?
#2

[eluser]Jondolar[/eluser]
You might want to turn on error reporting if it is disabled:
Also, try logging an error to see if the load->database is even returning.

http://ellislab.com/codeigniter/user-gui...rrors.html
#3

[eluser]grumble[/eluser]
Thanks for your suggestions Jondolar. I checked my main "index.php" file and I still have the default:
Code:
error_reporting(E_ALL);
I'm not sure what you mean by "logging an error". It seems clear to me that
Code:
$this->load->database();
is not returning, as "test 2" is not displayed.
#4

[eluser]grumble[/eluser]
He he he... Progress! First, I used
Code:
apt-get install php5-sqlite
to add an sqlite PDO driver to PHP. This appears to have done some good. Wow! apt-get is useful for something after all!? Now I get a rather useful error message:
Code:
A Database Error Occurred unable to open database: /var/www/CodeIgniter_1.7.1/&lt; my path&gt;
this shows me that CI has prefixed "/var/www/CodeIgniter_1.7.1/" to my database path. I had put my database in another directory for security reasons. I should be able to fix this now :coolsmile:
#5

[eluser]grumble[/eluser]
OK, so rather than using "~" in my path, I started with "/home/..." CI got the picture and didn't prefix my path. Unfortunately, I still got an error:
Code:
A Database Error Occurred
file is encrypted or is not a database
This feels like progress though. The thread:
http://ellislab.com/forums/viewthread/88885/ and the wiki
http://codeigniter.com/wiki/PDO_SQLite3/ suggests an alternative approach, using PDO as a database driver. In this approach, database.php would have:
Code:
$db['default']['database'] = "sqlite:/<path to database>/test.db3";
$db['default']['dbdriver'] = "pdo";
I will try this now as I have PDO working in straight PHP.
#6

[eluser]grumble[/eluser]
OK! After following the instructions at
http://codeigniter.com/wiki/PDO_SQLite3/
including all the extra files, and making the suggested changes to database.php, I finally got everything working!
I guess PDOs are a nice generic way of working with databases. I really don't like the look of some of the hacks in "pdo_driver.php" though.
I also think it sucks that it is so difficult to get CI working with sqlite3! Surely sqlite is a more appropriate "default" database!?




Theme © iAndrew 2016 - Forum software by © MyBB