Laravel - Read an SQLite file dynamically - php

I am trying to work on a project that accepts an SQLite file via upload, saves it, and parses the data in the SQLite database. I am finding that when I use a smaller DB file the "-wal" and "-shm" files are not being created but when using a larger DB (at least 2mb) they are.
Here is the code that I am using:
$destination = storage_path().'/tempDir/databases/'.$userId.'/';
//store the file in the directory
$request->file('database')->move($destination .'database/database.db');
$destination = $destination.'database/database.db';
Config::set('database.connections.sqlite.database', $dbLocation);
$sqliteDbConnection = \DB::connection('sqlite');
$getUsers = $sqliteDbConnection->table("users")->get();
Whenever I try and do this, I am getting the following error:
"SQLSTATE[HY000]: General error: 10 disk I/O error (SQL: select * from \"users\")"
Does anyone have an idea why that might be? I can confirm that the file being saved to the server is in good standing, I am able to open it with other SQLite viewers.
UPDATE: Is it possible that the link between Homestead and my MAC is prohibiting the file from being accessed by Laravel?
UPDATE2: The problem is 100% located in the sharing mechanism between VirtualBox and the computer. I tried this via XAMPP and it works alright. WTF?

This is how I managed to get a Builder instance with my custom PDO connection:
$pathname = '/absolute/path/to.sqlite';
$connection = new \Illuminate\Database\SQLiteConnection(new \PDO('sqlite:' . $pathname));
$builder = new \Illuminate\Database\Query\Builder($connection);
$builder->newQuery()->from('my_table')->get()->all();

Related

How can I open a SQLite3 file that's already in memory without ever touching the disk in PHP?

I have a SQLite3 file that's been read into memory (as a string) from the network. I want to open it and query it. I know SQLite3 can create empty in-memory DBs using the :memory: descriptor. But I want to know how to load in an existing DB without touching the disk.
Currently, I'm doing this:
$tempName = tempnam(sys_get_temp_dir(), 'sqlite3-');
$temp = fopen($tempName, 'w');
fwrite($temp, $cache->getDB($cacheKey));
fclose($temp);
// Delete the temp file when we're done.
register_shutdown_function('unlink', $tempName);
// ...
$db = new SQLite3($tempName, SQLITE3_OPEN_READONLY);
// Query the DB
$db->close();
This works fine, but it touches the disk unnecessarily.
I've already seen this answer, but that only covers C, not PHP.
I've also tried saving the file to php://memory, but SQLite3 is unable to open that.

ZF2, PGSQL - how to determine current save path of file?

I would like to save some data from pgsql database to a csv file. The thing you also should know that I'm working at ZF2 Framework. So the code looks like this:
$adapter = $this->serviceMenager->get('db');
$adapter->query("Copy (Select * From data.my_data) To '/test.csv' With CSV;")->execute();
But while I'm trying to execute the code, I'm getting exception:
SQLSTATE[42501]: Insufficient privilege: 7 ERROR: could not open file "/test.csv" for writing: Permission denied
That was the thing that I was expecting, because the only writing path is the public folder with privilages 777. But if I execute:
$adapter->query("Copy (Select * From data.my_data) To '/public/test.csv' With CSV;")->execute();
I'm getting exception:
SQLSTATE[58P01]: Undefined file: 7 ERROR: could not open file "/public/test.csv" for writing: No such file or directory
How to determine where am I (I mean current path)? And how to properly set the path in the query to export my data?
Thanks in advice.
It seems to be executed on PGSQL and not MYSQL as ins0 said.
The problem is almost what he stated though. You are trying to write to /test.csv, which means for PGSQL the root of the Unix server. You need to give the path you really want, and you can actually get the root of your app with getcwd.
Then your code will look like this:
$adapter = $this->serviceMenager->get('db');
$file = getcwd()."/public/test.csv";
$adapter->query("Copy (Select * From data.my_data) To '". $file ."' With CSV;")->execute();

Microsoft Excel cannot access the file

Microsoft Excel cannot access the file
'C:\xxx\test.xls'. There are several possible reasons: • The file name
or path does not exist. • The file is being used by another program. •
The workbook you are trying to save has the same name as a currently
open workbook.
I followed this steps
link
Also I included the extesion "php_com_dotnet.dll" in php.ini.
I have Zend Server CE with php 5.3.14
The issue persist when I reboot the computer.
code::
$file = "C:\\xxx\\test.xls";
try {
$excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!\r\n");
$excel->Visible = true;
$Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
} catch (Exception $exc) {
echo $exc->getMessage();
Things to consider:
Have you checked the permissions of the file? I'm assuming "xxx" is a placeholder for the proper folder name as well?
Do you have file open by something else? Excel perhaps? Excel will grab the file and "lock" it down from use by other things.
What user is the web server running as? Can it access that path?
try the answer from this guy named Aardigspook
in https://www.excelforum.com/excel-general/1182105-excel-cannot-access-the-file-2.html
The problem is a 'Name' . Go to the 'Formulas' tab, click 'Name Manager' and scroll down to the entry named 'special'. It refers to '--filepath--/[xxxxx.xlsx]Sheet1'!$A:$A'. Delete this, or amend it to a reference you have/want, and the error should be gone.
-captain hair.
After searching for a week and 3 times a new office 365 installation, my solution was to replace my faulty Micro-SD adapter.

SugarCRM save database data to file

Is there any way I can save the data of a specific table of the sugarcrm database into a doc file ?
I have a custom module with username,some notes and date. I want to write this into the database and into a file as well.
Its not just a php file. I want to use logic hooks and write the code. I want to use the logic hooks to access database and then write the data into the file.
Thanks in advance
Saving as a DOC file probably isn't the best idea, since it is primarily used for formatting information. A standard .txt file is usually what you would use for such a process.
With that said, there isn't any methods built into sugar that will let you do this. You will need to build the capability into the module.
What exactly are you attempting to accomplish? There is a very powerful auditing tool set, which is good for seeing revisions to a module object. If you are just wanting to monitor changes to the table, you can setup logging for that table/database inside of SQL.
+++Ok, if you are just looking to write to a file after saves, follow the instructions at: http://cheleguanaco.blogspot.com/2009/06/simple-sugarcrm-logic-hook-example.html for a quick how-to on getting the logic hooks working. You are going to want to make a php file that simply uses the data passed to it via the bean class, and either writes to the file directly from the data within bean, or uses the bean->id parameter to do a SQL query and write to the file from that data.
Also, is this a DOC file that is going to be immediately generated and then destroyed at the end of the transaction? Or is it more of a log file that will be persistent?
++++That is simple enough then
Where you have the Query right now, replace it with:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
*A quick note, you might need to set permissions in order to be able to read/write to the file, but those are specific to the machine type, so if you encounter errors with either do a search for setting permissions for your particular server type.
**Also, 'SELECT * FROM database.table' is going to return ALL of the rows in the entire table. This will generate a very large file, and be a performance hindrance as the table grows. You should use the bean class to update the last saved tuple:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table WHERE id = '".$focus->id."';";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
You can export/dump mysql databases into SQL files using mysqldump
mysqldump -u userName -p databaseName tableName > fileName.sql

Download file which was upload to mysql to server directory

I've searched and searched but have not found a script capable of downloading files which are uploaded to my mysql database down to a directory in my server.
This is only temporary and once the file has been used it'll need to be disposed of.
Got any ideas?
I suppose you meant that the file is saved into mysql database like BLOB (binary data)
$result = mysql_query("select * from tablename where id=XXXX");
if (mysql_num_rows($result)>0){
$r = mysql_fetch_array($result,MYSQL_ASSOC);
file_put_contents($filename,$r['column_blob']);
}
I used file_put_contents function that it is binary safe, but there is also fwrite

Categories