Error accessing an SQLite db from a different location using PHP - php

I'm stuck with this problem. I'm trying to access an sqlite database from a different location. So far I have tried using PDO to access such file. I've tried using PDO since from what I read here on SO that would be easier. I only want to do some select queries on the database.
try{
$DBH = new PDO("sqlite:192.168.48.52/data/log/localdb.db");
echo "connected";
}
catch(PDOException $e) {
echo $e->getMessage();
}
it outputs an error message saying 'SQLSTATE[HY000] [14] unable to open database file'. I could access the location via SSH/putty or manually input the directory through a web browser. But not through PHP coded location. The file resides actually in a gsm gateway interface which has a -rw-r--r-- permission.
If I were to actually download the file and put it in my htdocs folder it does open the file and echos 'connected'
$DBH = new PDO("sqlite:localdb.db"); <--- **like this**
I also tried doing this since it requires some authentication to access the directory.
$DBH = new PDO("sqlite:admin:admin#192.168.48.52/data/log/localdb.db");
to no avail.
Thanks in advance.

i think you miss 1 colon after the IP :
$DBH = new PDO("sqlite:admin:admin#192.168.48.52:/data/log/localdb.db");

Related

Laravel Import SQLite

I am trying to import an SQLite file in my Laravel project which I am importing and storing at runtime. When I try, I find that sometimes it works and other times it does not. Here is the code that I am using to generate the PDO connection:
try{
$db = new PDO('sqlite:'.$dbSavedLocation);
return $db;
}
catch(\PDOException $e){
LogRepo::logError(0, $e->getMessage(), 1, $e->getFile(), $e->getLine());
return null;
}
"SMS Database process error: SQLSTATE[HY000] [14] unable to open database file" when trying to open the file?
Why would this not work? What would cause the error of
Is the .sqlite file opened by any other programs?
I've had it happen a few times where I've had it open in SQLiteBrowser and gotten a similar error, but having it open elsewhere will prevent Laravel from being able to open the database.

SQLite/PDO - How to use/connect to database file (.db) on localhost (XAMPP)?

I use XAMPP with
PHP Version 5.4.7
PDO drivers - mysql, sqlite - enabled
SQLite Library - 3.7.7.1 - enabled
sqlite3 - SQLite3 module version - 0.7 - enabled
sqlite3.extension_dir - no value - no value
API Extensions - mysql,mysqli,pdo_mysql
So, if I understood well some tutorials, that's all I need to be able to start working.
So, in C:\xampp\htdocs\my_project I copied database.db file (sakila downloaded from here: https://github.com/joshuakleveter/sakila_db/tree/master/public_html )
Then I created index.php that looks like this:
<?php
try {
$db = new PDO('sqlite:./database.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(Exception $e) {
echo 'Error: ';
echo $e->getMessage();
die();
}
and when I run this there are no errors. As you can see, my path is /database.db. If I change it to just
$db = new PDO('sqlite:.database.db');
no errors. Or without dot:
$db = new PDO('sqlite:database.db');
or absolute path - there are no errors.
Even worse, if I put wrong not existing .db name:
$db = new PDO('sqlite:daaaaaaaaaaaatabase.db');
also - there are no errors.
Can you tell me what am I doing wrong?
With all of the above paths I've tried the following:
$results = $db->query('select * from film');
var_dump($results);
die();
and I get "General error: 1 no such table", but the database.db has that table (I installed sqlite, opened that database in command line and checked - the database.db is OK).
Connecting to a not yet existing file is how new databases are created.
Relative paths are relative to the current directory, which you usually cannot control, except by setting it to some absoulte path. Just use an absolute path directly when opening the DB.

Cannot access SQLite3 db using php

I've spent many-a hour trying to get a local webserver working (I'm new)
I created a sqlite3 database ('database.sql') in the www folder, and tried calling it with numerous different php commands (php 5.5, I checked) such as '$test = new SQLite('database.sql')' or the same with SQLite3, both with no luck. Also tried $test->open('database.sql').
Always with the fatal error "Class 'SQLite' not found". I've spent too many hours on what I'm sure is a very simple problem, I'm sorry to have to ask this!
To open a DB using PHP5 and SQLite we need to use a PDO and not the sqlite_open() function.
This is how to open or create a database: (not sure if it's bug free)
try {
/*** connect to SQLite database ***/
$dbh = new PDO("sqlite:VPN0.sqlite");
echo "Handle has been created ...... <br>";
}
catch(PDOException $e) {
echo $e->getMessage();
echo "<br>Database is loaded UNSUCCESSFULLY .. ";
die("<br>Query is closed $error");
}
echo "Database loaded SUCCESSFULLY ....";
Hope this helps!
There is a php package called sqlite3 which you can use (as well as PDO which is given above). Here is a fraction of code which uses it.
$db = new SQLite3(DATABASE);
if (isset($dbversion)) { //only newer versions of chat will have this
$version = $db->querySingle("SELECT value FROM parameters WHERE name = 'db_version'");
Where the DATABASE variable has been defined with
define('DATA_DIR',$datadir); //Should be outside of web space
define('DATABASE',DATA_DIR.'chat.db');
Are you sure that the package is installed on your server?
If you're still having problems you can do a couple of things. First, use phpinfo(); in a page to determine if the SQLite3 extension is installed.
Second, if you want to use PDO make sure that the following line in your php.ini is un-commented:
extension=php_pdo_sqlite.dll
If you have to un-comment this you will need to restart your server for the changes to take effect.

PDO connect to SQLite server on different server

I have a very simple SQLite database that I need to read/write to from a different server.
Say the database is stored here : http://www.abc.com/data/data.sqlite
And I'm using PHP to access it from http://www.xyz.com
So my first attempt was the following:
$dbpath = "http://www.abc.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
No good, I get the following:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [14] unable to open database file'.........PDO->__construct('sqlite:http://w...') #1 {main} thrown
If try and copy the database onto the same server I'm accessing from:
$dbpath = "http://www.xyz.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
I get the same message.
It's only when I give it a relative path on the same server:
$dbpath = "../data/data.sqlite";
That it actually works.
I know the database URLs and database itself are correct.
So is there a limitation to accessing cross-servers? Anyone know out to solve this issue?
Thanks a lot.
There are no such thing like 'SQLite server'. It exists only in a form of a file.
But there are no files in the HTTP protocol, but URIs only.
So, this is essential incompatibility.
To be able to do remote calls you have 3 choices
Only as a joke: download the file locally before each SELECT query and upload it back after updates
Establish some proxy script, to recieve a query and to return json.
Get yourself a real database server.
Or just change the project architecture to eliminate the need of remote access.

php dbase error when opening a .dbf file

I'm trying to use the dbase library in php5.3 to open a .dbf file. I've got the dbase.so library installed and active on my php5 build and I'm executing the following code:
$db = dbase_open('CMX.dbf', 0);
if( $db ){
echo 'success';
dbase_close($db);
}
Where CMX.dbf is a Visual FoxPro9 data table and is located in the same directory as the executing script with read/write/execute permissions enabled.
The following is an exert from /var/log/apache2/error.log:
PHP Warning: dbase_open(): unable to open database CMX.dbf in /var/www/test.php on
line 28
As this error/warning is not very descriptive, I'm having issues tracking down the root cause. Can anyone help with this?
Not positive about PHP, nor Apache, but typically, when trying to connect to database files (or Foxpro), the typical approach would be to have a CONNECTION to a PATH, then perform a query against the name of the table....
Try this (in foxpro):
use cmx.dbf
copy to cmx_php.dbf type fox2x
I was having a similar problem where some dbs would open and others would not. This allowed me to access the db with php/dbase.so
I found the info here in the comments section.
Try this:
$db_path = "CMX.dbf";
$db = dbase_open($db_path, 0) // 0=ReadOnly, 1=WriteOnly 2=ReadWrite
or die("Error! Could not open dbase database file '$db_path'.");
if( $db ){
echo 'success';
dbase_close($db);
}

Categories