Sqlite: How to cast(data as TEXT) for BLOB - sqlite

I have a sqlite database from which I want to extract a column of information with the datatype BLOB. I am trying this:
SELECT cast(data as TEXT) FROM content
This is obviously not working. The output is garbled text like this:
x��Uak�0�>�8�0Ff;I�.��.i׮%�A��s�M
The data in the content column is mostly text, but may also have images (which I recognized could cause a problem if I cast as TEXT). I simply want to extract that data into a usable format. Any ideas?

You can use
SELECT hex(data) FROM content
or
SELECT quote(data) FROM content
The first will return a hex string (ABCD), the second quoted as an SQL literal (X'ABCD').
Note that there's (currently) no way of converting hexadecimal column information back to a BLOB in SQLite. You will have to use C/Perl/Python/… bindings to convert and import those.

You can write some simple script which will save all blobs from your database into files. Later, you can take a look at these files and decide what to do with them.
For example, this Perl script will create lots of files in current directory which will contain your data blob fields. Simply adjust SELECT statement to limit fetched rows as you need:
use DBI;
my $dbh = DBI->connect("dbi:SQLite:mysqlite.db")
or die DBI::errstr();
my $sth = $dbh->prepare(qq{
SELECT id, data FROM content
});
$sth->execute();
while (my $row = $sth->fetchrow_hashref()) {
# Create file with name of $row->{id}:
open FILE, ">", "$row->{id}";
# Save blob data into this file:
print FILE $row->{data};
close FILE;
}
$dbh->disconnect();

Related

How can we insert an image in sqlite database(table)?

I guess that it's valid for MySQL, however, I cannot find anything about SQLite.
Basically, I have a table which is named 'CUSTOMER'.
So I create an attribute like this:
.. Image BLOB .. after that my insert statement looks like this:
INSERT INTO CUSTOMER(1,LOAD_FILE(D:/Project/Images/X.jpg));
However, the LOAD_FILE tag is not working and I don't know how to insert an image or if we can do that.
If you're using the sqlite3 shell, the relevant function is readfile().
If you're doing this from your own program, you have to read the file into a byte array and bind it as a blob to the desired column in an insert. The exact details vary depending on language and sqlite bindings, but you shouldn't ever have to convert it to a blob literal string and embed that directly into a statement.
You can store an image as a BLOB, but you'd have to insert it as a a series of bytes using something like :-
INSERT INTO CUSTOMER (image_column, other_column) VALUES(x'0001020304........','data for the first other column');
So you'd need to convert the file into a hex string to save it.
However, it's not really recommended to store images but to rather store the path to the image and then retrieve the file when you want to display/use the image.
Saying that, SQLite can, for smaller images (say 100K), actually be more efficient 35% Faster Than The Filesystem.
You must use the cmd command line (windows) to insert the attachment. The sqllitespy (version 1.9.13) does not support de command from the program command line.
You should acess you database first with the CMD and after that;
update (your table) set (column) = readfile ('dir where the files are stored'||num||´.jpg);

SQLite Select Last Occurance of Character

I'm using SQLite, and I'm unable to find a way to locate the index of the last occurrence of a character. For example, the records that I need to parse are:
test123.contoso.txt
testABC.contoso.atlanta.docx
another.test.vb
I would appreciate if anybody can point me in the direction how I can parse the file extensions (txt, docx, vb) from these records through a SQLite query. I've tried using the REVERSE function, but unfortunately SQLite doesn't include this in it's toolbox.
You can adapt the solution in How to get the last index of a substring in SQLite? to extract the extension.
select distinct replace(file, rtrim(file, replace(file, '.', '')), '') from files;
If you want to check whether a file name has a specific extension, you can use LIKE:
... WHERE FileName LIKE '%.txt'
However, it is not possible with the built-in functions to extract the file extension.
If you need to handle the file extension separately, you should store it separately in the database, too.

Dynamically generating pzPVStream from a view

Is there some way to create a view that returns a pzPVStream that can be natively parsed by Pega when it executes an RDB?
For instance, maybe a query (in MS SQL Server) that resembled:
SELECT test_tbl_outer.ID, (
select *, 'My-Int-TestClass' as "pxObjClass"
from {class:My-Int-TestClass} as test_tbl_inner
where test_tbl_inner.ID=test_tbl_outer.ID
FOR XML RAW('pagedata'), TYPE, ELEMENTS
) as pzPVStream
from {class:My-Int-TestClass} as test_tbl_outer
This gets an invalid signature error (the SQL query does work directly however), and if I try to shove a signature string onto the column ('PR6d' or previous) I just get a different error regarding headers.
So at this point, I do realize that the pzPVstream is not stored as xml but as some sort of packed & compressed string. Is there a way for me to create a valid pzPVstream on the fly? Maybe something similar to what pr_read_from_stream does but in reverse?
The use case is that we'd like to pull a whole mess of data from an existing data warehouse. And it would be nice if we could pull all the multi-value data (many,many joins deep) over in one trip. We are not too concerned with the size of this object as we plan on pulling this data one way or another.
The pzPvStream is a compressed blob and it resonates a work object. It is compressed and stored as a single column in a table.
When it is read using obj-browse or obj-open activities, the blob is decompressed and all the encompassed properties are mapped to the clipboard.
This value has a proprietary format; the values are obfuscated.

Empty fields extracted from excel

This is my problem:
I'm reading data from an Excel file on a .NET MVC app, what I'm doing is to read all data from the excel and then loop over each record inserting the data contained in the record into my business model.
All works perfectly. However, I've found that one field, sometimes, return an empty string when retrieved from the excel. Curiously this field can contain a simple string or a string that will be treated as an array (it can include '|' characters to build the array) on some excel files the field returns empty when the '|' char is present and in others when it isn't, and this behaviour is consistent all along that file.
There are other fields that can receive the separator and work always ok. The only difference between both fields are that the working ones are pure strings and the one that's failing is a string of numbers with possibles '|' separating them.
I've tried to change the separator character (I tried with '#' with same results) and to specifically format the cells as text without any success.
This is the method that extracts data from the excel
private DataSet queryData(OleDbConnection objConn) {
string strConString = "SELECT * FROM [Hoja1$] WHERE NUMACCION <> ''";
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset = new DataSet();
objAdapter1.Fill(objDataset, "ExcelData");
return objDataset;
}
I first check the fields from the excel with:
fieldsDictionary.Add("Hours", table.Columns["HOURS"].Ordinal);
And later, when looping through the DataSet I extract data with:
string hourString = row.ItemArray[fieldsDictionary["Hours"]].ToString();
This hourString is empty in some records. In some Excel files it's empty when the record contains '|', on others it's empty when it doesn't. I haven't found yet a file where it returns empty on records of both classes.
I'm quite confused about this. I'm pretty sure it has to be related to the numerical nature of field data, but cannot understand why it doesn't solve when I force the cells on the excel file to be "text"
Any help will be more than welcome.
Ok. I finally solved this.
It seems like Excel isn't able to recognize a whole column as same data type if it contains data of possibly different classes. This happens even if you force the cell format to be text on the workbook, as when you query the data it will recognize the field as a determinated type according to the first record it receives; that was the reason why different files emptied different type of records, files starting with a plain text emptied numeric values and vice versa.
I've found a solution to this just changing the connection string to Excel.
This was my original connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pathToFile;Extended Properties="Excel 8.0;HDR=Yes;"
And this the one that fixes the problem
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pathToFile;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1"
The parameter IMEX=1 states to excel that it must manage all mixed data columns as plain text. This won't work for you if you need to edit the excel file, as this parameter also opens it on read-only mode. However it was perfect for my situation.

Using Full text search on varbinary data type for searching file contents

I create a full-text search index on varbinary(max) column and insert file content into this column.(files like docx,xlsx,txt,...) I create an extension column for specify content type.but when I query this table with contains,
SELECT *
FROM tblFiles
WHERE CONTAINS(tblFiles.FileContent, 'web OR webserver');
no result were showing.why?
You may need to wait a bit after defining a full text index, because the index itself is created in the background; it's an async process, not synchronous. For a small table it may not be noticeable, but for a large one, it will be.
Also, make sure the types in your file type column start with a dot: .docx instead of just docx.
I found why my query dont work. because files that inserted into db was docx and in my database does not exist any filter for this format.I inserted a doc document and query it.It works. :)
Thank you.

Resources