How to search for a specific BLOB in SQLite? - vb.net

I write binary data from pictures into my SQLite database into a BLOB field called "icondata".
CREATE TABLE pictures(id INTEGER PRIMARY KEY AUTOINCREMENT, icondata BLOB)
The code snippet to write the binary data is:
SQLcommand.CommandText = "INSERT INTO pictures (id, icondata) VALUES (" & MyInteger & "," & "#img)"
SQLcommand.Prepare()
SQLcommand.Parameters.Add("#img", DbType.Binary, PicData.Length)
SQLcommand.Parameters("#img").Value = PicData
This works fine and I can find the BLOB values in the database using a tool like SQlite Spy.
But how can I search for a specific "icondata" BLOB?
If I try it with:
SQLcommand.CommandText = "SELECT id FROM pictures WHERE icondata=#img"
and with the same Parameters like above:
SQLcommand.Prepare()
SQLcommand.Parameters.Add("#img", DbType.Binary, PicData.Length)
SQLcommand.Parameters("#img").Value = PicData
Dim SQLreader As SQLite.SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
... Process found database entries....
End While
I don't get any result.
If I change the SELECT query to 'LIKE' instead of '=' I get all entries with BLOBs, not only the matching one.
How do I have to write the SELECT query to find the 1 exactly matching entry for a specific BLOB?

You can search on BLOBs here's some examples :-
DROP TABLE IF EXISTS pictures;
CREATE TABLE IF NOT EXISTS pictures (id INTEGER PRIMARY KEY, icondata columntypedoesnotmatter);
INSERT INTO pictures (icondata) VALUES
(x'fff1f2f3f4f5f6f7f8f9f0fff1f2f3f4f5f6f7f8f9f0'),
(x'ffffffffffffffffffff'),
(x'010203040506070809'),
(x'010203040506070809010203040506070809')
;
SELECT id, hex(icondata) FROM pictures WHERE icondata = x'010203040506070809' OR icondata = x'FFFFFFFFFFFFFFFFFFFF';
SELECT id, hex(icondata) FROM pictures WHERE icondata LIKE '%'||x'F0'||'%';
SELECT id, hex(icondata) FROM pictures WHERE hex(icondata) LIKE '%F0%';
This results in :-
First query as expected finds the 2 rows (2nd and 3rd)
i.e. SELECT id, hex(icondata) FROM pictures WHERE icondata = x'010203040506070809' OR icondata = x'FFFFFFFFFFFFFFFFFFFF'; results in :-
The Second query does not work as expected and is an example of how not to use LIKE :-
i.e. SELECT id, hex(icondata) FROM pictures WHERE icondata LIKE '%'||x'F0'||'%'; results in the unexpected two rows :-
I believe that this behaviour is due to only checking the higher/lower order bits but I can't find the relevant documentation.
The third query, however converts the stored blob into it's hexadecimal string representation using the hex function and compares that against the string representation of F0
i.e. SELECT id, hex(icondata) FROM pictures WHERE hex(icondata) LIKE '%F0%'; results in the expected single row :-
I've never used vb.net so I'm not sure how you'd code the above as passed parameters.

Related

Select Specific Column from a Linked Server Table

I have the following C# code to select a column from a table that is on a linked server:
var query2 = $#"select [FileName] from [AMS_H2H].[H2H].[dbo].[FileReconciliation] where ProductCode = #productCode";
LayZConnection(); //make the db connection
var candidates = _dbConnection.Query<int>(query2, new { productCode = "ACHDH" });
When running it, I get the following error:
"Input string was not in a correct format."
If my query is instead the following, where I select all columns, it works:
var query2 = $#"select * from [AMS_H2H].[H2H].[dbo].[FileReconciliation]
What is the correct format to select just the FileName. Btw, the first query works fine from MSSMS.
You're specifying a type of int in Query<int>, which will cause Dapper to try and map the result of the query to an integer, however your query is returning a filename in select [FileName], which would suggest that it is a string.
Changing the type Query<string> should solve the issue.
More information on Dapper's Query method is available in Dapper's documentation

I want to sql script input string and value in database is number?

I want to sql script in select
input string and value in database is number?
EX.
Input: Wait Approve
ApproveStatus(nvarchar)
"0"
"2"
"4"...
ApproveStatus = 2 It's "Wait Approve"
I want sql script find value "Wait Approve" in ApproveStatus and Output is fields ApproveStatus = 2
This first in questions
Something like this?
Given the following data:
CREATE TABLE statuslookup (
id int unique,
label nvarchar unique
);
INSERT INTO statuslookup (id, label) values (2, 'Wait Approve');
Then if you need to convert from label to id:
SELECT id FROM statuslookup WHERE label = 'Wait Approve';
Hopefully that at least gets you started.....

Update all rows where contains 5 keys

I have Ticket table that has some columns like this :
ID : int
Body : nvarchar
Type : int
I have many rows where the Body column has value like this :
IPAddress = sometext, ComputerName = sometext , GetID = sometext, CustomerName=sometext-sometext , PharmacyCode = 13162900
I want update all rows' Type column where the Body column has at least five of the following keys:
IPAddress, ComputerName, GetID, CustomerName, PharmacyCode
You could do it with a simple update statement like that
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%'
and Body LIKE '%ComputerName%'
and Body LIKE '%GetID%'
and Body LIKE '%CustomerName%'
and Body LIKE '%PharmacyCode%'
if you know the 'keys' are always in the same order you could concatenate the LIKE conditions like so
UPDATE Ticket
SET Type = 4
WHERE Body LIKE '%IPAddress%ComputerName%GetID%CustomerName%PharmacyCode%'
If you have the possibility to change the data model it would be much better to explode this key & value column into an own table and link it back to this table as it is done in a proper relational model.
If you could calculate number of key value pair by number of = present in your string you could use this query
Update tblname set col=val where len(colname) - len(replace(colname,'=','')>5
The where part actually gives number of equal signs present in your string.

How to find two strings in a CLOB column?

Ive tried many queries to find... just one word and I can´t even make that.
Its a DB2 database Im using com.ibm.db2.jcc.DB2Driver
This brings me info:
select *
from JL_ENR
where id_ws = '002'
and dc_dy_bsn = '2014-08-25'
and ai_trn = 2331
the JL_TPE column is the CLOB column where I want to find two strings in that search result ( and dc_dy_bsn = '2014-08-25'
and ai_trn = 2331 ).
So first I tried with one:
select
dbms_lob.substr(clob_column,dbms_lob_instr(JL_TPE,'CEMENTO'),1)
from
JL_ENR
where
dbms_lob.instr(JL_TPE,'CEMENTO')>0;
didnt work
SELECT * FROM JL_ENR WHERE dbms_lob.instr(JL_TPE,'CEMENTO')>0
and ai_trn = 2331
and dc_dy_bsn = '2014-08-25'
didnt work
Select *
From JL_ENR
Where NOT
DBMS_LOB.INSTR(JL_TPE, 'CEMENTO', 1, 1) = 0;
didn´t work
Could someone explain me how to find two strings please?
Or a tutorial link where it is explained how to make it work...
Thanks.
Can you provide some sample data and the version you are using? Your example should work (tested on v10.5.0.1):
db2 "create table test ( x int, y clob(1M) )"
db2 "insert into test (x,y) values (1,cast('The string to find is CEMENTO, how do we do that?')"
db2 "insert into test (x,y) values (2,cast('The string to find is CEMENT, how do we do that?' as clob))"
db2 "select x, DBMS_LOB.INSTR(y, 'CEMENTO', 1) from test where DBMS_LOB.INSTR(y, 'CEMENTO', 1) > 0"
X 2
----------- -----------
1 23
1 record(s) selected.
I had to search for a specific value in the where clause. I used TEXTBLOB LIKE '%Search value%' and it worked! This was for db2 in a CLOB(536870912) column.

dataSet.xsd query select where in

In SQL it works fine
SELECT NOID, NO_DOSSOIN, NO_ORDO, POSOLOG FROM dbo.ESPMEDS_ORDO_SORTIR
WHERE NO_DOSSOIN = #NO_DOSSOIN AND NOID IN (#NOIDIN)
example
SELECT NOID, NO_DOSSOIN, NO_ORDO, POSOLOG FROM dbo.ESPMEDS_ORDO_SORTIR
WHERE NO_DOSSOIN = 10 AND NOID IN (16,17)
But as I put this in a dataset.xsd query I don't get the same output, I cannot put more than one id into NOIDIN parameter because the NOID type is integer
so my file DataSet.xsd only work like this:
SELECT NOID, NO_DOSSOIN, NO_ORDO, POSOLOG FROM dbo.ESPMEDS_ORDO_SORTIR
WHERE NO_DOSSOIN = 10 AND NOID IN (16)
the error says I cannot convert data from string to int
You should just separate the NOIDIN. Don't expect to be able to pass an Int32 that looks like 16,17 it will always be seen as a string by this wizard and won't compile at all if you execute it from the code.
The easiest option for you is to pass the range in two values like this :
SELECT NOID, NO_DOSSOIN, NO_ORDO, POSOLOG FROM dbo.ESPMEDS_ORDO_SORTIR
WHERE NO_DOSSOIN = #NO_DOSSOIN AND NOID IN (#NOIDSTART, #NOIDEND)
And then assign :
#NOIDSTART = 16
#NOIDEND = 17
If you're parameters are dynamic you should read this article which pretty much covers the subject.