S
S
Savely2020-09-28 13:58:03
SQL
Savely, 2020-09-28 13:58:03

SqlLite. How to use the like operator?

Hello!
I'm trying to use like in sqllit and I can't make a selection from the param field.
When selecting, this field is shown as:
b'{"param1":"1","param2":"2"}'
And if you write a query like, nothing will be returned (( For other fields, like works fine. Tell me how to treat Important note - I use either for python sqlite3
select * from table where param like "%2%",

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saveliy, 2020-09-28
@aaland

They suggested on a neighboring site that I do not have a text field, but a BLOB
The solution is an explicit conversion (CAST).

SELECT * FROM table WHERE CAST(param AS TEXT) LIKE '%2%';

A
Alexander, 2020-09-28
@shabelski89

Why do you need to store a dictionary as a byte string in a table?

import sqlite3


blob = bytearray(b'{"param1":"1","param2":"2"}')

with sqlite3.connect('np.db') as connection:
    cursor = connection.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS t_info_t (blob_Value BLOB )")
    cursor.execute("INSERT OR IGNORE INTO t_info_t (blob_Value) values (?)", (blob,))
    cursor.execute("SELECT * FROM t_info_t")
    data = cursor.fetchall()
    print(data)
    print(type(data[0][0]))
    out = data[0][0].decode()
    print(out)
    print(type(out))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question