Don't use string interpolation. Use SQL parameters:
Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, ?)", (blob, ))
Demo:
>>> Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, ?)", (blob,))
<sqlite3.Cursor object at 0x10a0ca810>
>>> Con.commit()
>>> Cur = Con.cursor()
>>> Cur.execute('select * from t_info')
<sqlite3.Cursor object at 0x10a0caab0>
>>> list(Cur)
[(1, b'Silicon Valley (TV series)\r\nFrom Wikipedia, the free encyclopedia\r\nSilicon Valley is an American television comedy series create')]
You can use Martijn's answer to insert correctly but in general if you are using a try/except
catch the exceptions you expect not every exception, if you did your tostring
error would have been obvious:
try:
Cur.execute("insert into t_info (Primary_Key, Info_Value) values (1, X'" + blob.tostring() + "')")
except sqlite3.Error as e:
print(e)
Which would give you:
AttributeError: 'bytearray'
object has no attribute 'tostring'
SQLite is a C library that provides a lightweight disk-based database that doesn’t require a separate server process and allows accessing the database using a nonstandard variant of the SQL query language. Some applications can use SQLite for internal data storage. It’s also possible to prototype an application using SQLite and then port the code to a larger database such as PostgreSQL or Oracle.,Exception raised for errors caused by problems with the processed data, like numeric values out of range, and strings which are too long. DataError is a subclass of DatabaseError.,A callable that accepts a bytes parameter and returns a text representation of it. The callable is invoked for SQLite values with the TEXT data type. By default, this attribute is set to str. If you want to return bytes instead, set text_factory to bytes.,You’ve now created an SQLite database using the sqlite3 module, inserted data and retrieved values from it in multiple ways.
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("CREATE TABLE movie(title, year, score)")
>>> res = cur.execute("SELECT name FROM sqlite_master") >>>
res.fetchone()
('movie', )
>>> res = cur.execute("SELECT name FROM sqlite_master WHERE name='spam'") >>>
res.fetchone() is None
True
cur.execute("" " INSERT INTO movie VALUES('Monty Python and the Holy Grail', 1975, 8.2), ('And Now for Something Completely Different', 1971, 7.5) "" ")
Each column in an SQLite 3 database is assigned one of the following type affinities:,Every table column has a type affinity (one of BLOB, TEXT, INTEGER, REAL, or NUMERIC) but expressions do not necessarily have an affinity. ,If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity BLOB.,If the declared type for a column contains the string "BLOB" or if no type is specified then the column has affinity BLOB.
CREATE TABLE t1(a INT, b VARCHAR(10));
INSERT INTO t1(a, b) VALUES('123', 456);
CREATE TABLE t1(a INT, b TEXT, c REAL); CREATE VIEW v1(x, y, z) AS SELECT b, a + c, 42 FROM t1 WHERE b != 11;
CREATE TABLE t1(
t TEXT, --text affinity by rule 2 nu NUMERIC, --numeric affinity by rule 5 i INTEGER, --integer affinity by rule 1 r REAL, --real affinity by rule 4 no BLOB--no affinity by rule 3
);
--Values stored as TEXT, INTEGER, INTEGER, REAL, TEXT.
INSERT INTO t1 VALUES('500.0', '500.0', '500.0', '500.0', '500.0');
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
text | integer | integer | real | text
--Values stored as TEXT, INTEGER, INTEGER, REAL, REAL.
DELETE FROM t1;
INSERT INTO t1 VALUES(500.0, 500.0, 500.0, 500.0, 500.0);
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
text | integer | integer | real | real
--Values stored as TEXT, INTEGER, INTEGER, REAL, INTEGER.
DELETE FROM t1;
INSERT INTO t1 VALUES(500, 500, 500, 500, 500);
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
text | integer | integer | real | integer
--BLOBs are always stored as BLOBs regardless of column affinity.
DELETE FROM t1;
INSERT INTO t1 VALUES(x '0500', x '0500', x '0500', x '0500', x '0500');
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
blob | blob | blob | blob | blob
--NULLs are also unaffected by affinity
DELETE FROM t1;
INSERT INTO t1 VALUES(NULL, NULL, NULL, NULL, NULL);
SELECT typeof(t), typeof(nu), typeof(i), typeof(r), typeof(no) FROM t1;
null | null | null | null | null
CREATE TABLE t1(
a TEXT, --text affinity b NUMERIC, --numeric affinity c BLOB, --no affinity d--no affinity
);
--Values will be stored as TEXT, INTEGER, TEXT, and INTEGER respectively
INSERT INTO t1 VALUES('500', '500', '500', 500);
SELECT typeof(a), typeof(b), typeof(c), typeof(d) FROM t1;
text | integer | text | integer
--Because column "a"
has text affinity, numeric values on the
--right - hand side of the comparisons are converted to text before
--the comparison occurs.
SELECT a < 40, a < 60, a < 600 FROM t1;
0 | 1 | 1
--Text affinity is applied to the right - hand operands but since
--they are already TEXT this is a no - op;
no conversions occur.
SELECT a < '40', a < '60', a < '600'
FROM t1;
0 | 1 | 1
--Column "b"
has numeric affinity and so numeric affinity is applied
--to the operands on the right.Since the operands are already numeric,
--the application of affinity is a no - op;
no conversions occur.All
--values are compared numerically.
SELECT b < 40, b < 60, b < 600 FROM t1;
0 | 0 | 1
--Numeric affinity is applied to operands on the right, converting them
--from text to integers.Then a numeric comparison occurs.
SELECT b < '40', b < '60', b < '600'
FROM t1;
0 | 0 | 1
--No affinity conversions occur.Right - hand side values all have
--storage class INTEGER which are always less than the TEXT values
--on the left.
SELECT c < 40, c < 60, c < 600 FROM t1;
0 | 0 | 0
--No affinity conversions occur.Values are compared as TEXT.
SELECT c < '40', c < '60', c < '600'
FROM t1;
0 | 1 | 1
--No affinity conversions occur.Right - hand side values all have
--storage class INTEGER which compare numerically with the INTEGER
--values on the left.
SELECT d < 40, d < 60, d < 600 FROM t1;
0 | 0 | 1
--No affinity conversions occur.INTEGER values on the left are
--always less than TEXT values on the right.
SELECT d < '40', d < '60', d < '600'
FROM t1;
1 | 1 | 1
CREATE TABLE t1(
x INTEGER PRIMARY KEY,
a, /* collating sequence BINARY */
b COLLATE BINARY, /* collating sequence BINARY */
c COLLATE RTRIM, /* collating sequence RTRIM */
d COLLATE NOCASE /* collating sequence NOCASE */
);
/* x a b c d */
INSERT INTO t1 VALUES(1, 'abc', 'abc', 'abc ', 'abc');
INSERT INTO t1 VALUES(2, 'abc', 'abc', 'abc', 'ABC');
INSERT INTO t1 VALUES(3, 'abc', 'abc', 'abc ', 'Abc');
INSERT INTO t1 VALUES(4, 'abc', 'abc ', 'ABC', 'abc');
/* Text comparison a=b is performed using the BINARY collating sequence. */
SELECT x FROM t1 WHERE a = b ORDER BY x;
--result 1 2 3
/* Text comparison a=b is performed using the RTRIM collating sequence. */
SELECT x FROM t1 WHERE a = b COLLATE RTRIM ORDER BY x;
--result 1 2 3 4
/* Text comparison d=a is performed using the NOCASE collating sequence. */
SELECT x FROM t1 WHERE d = a ORDER BY x;
--result 1 2 3 4
/* Text comparison a=d is performed using the BINARY collating sequence. */
SELECT x FROM t1 WHERE a = d ORDER BY x;
--result 1 4
/* Text comparison 'abc'=c is performed using the RTRIM collating sequence. */
SELECT x FROM t1 WHERE 'abc' = c ORDER BY x;
--result 1 2 3
/* Text comparison c='abc' is performed using the RTRIM collating sequence. */
SELECT x FROM t1 WHERE c = 'abc'
ORDER BY x;
--result 1 2 3
/* Grouping is performed using the NOCASE collating sequence (Values
** 'abc', 'ABC', and 'Abc' are placed in the same group). */
SELECT count( * ) FROM t1 GROUP BY d ORDER BY 1;
--result 4
/* Grouping is performed using the BINARY collating sequence. 'abc' and
** 'ABC' and 'Abc' form different groups */
SELECT count( * ) FROM t1 GROUP BY(d || '') ORDER BY 1;
--result 1 1 2
/* Sorting or column c is performed using the RTRIM collating sequence. */
SELECT x FROM t1 ORDER BY c, x;
--result 4 1 2 3
/* Sorting of (c||'') is performed using the BINARY collating sequence. */
SELECT x FROM t1 ORDER BY(c || ''), x;
--result 4 2 3 1
/* Sorting of column c is performed using the NOCASE collating sequence. */
SELECT x FROM t1 ORDER BY c COLLATE NOCASE, x;
--result 2 4 3 1
Marketplace
$ mkdir sqlite3_blob_data $ cd sqlite3_blob_data
$ md sqlite3_blob_data $ cd sqlite3_blob_data
import os
import sqlite3
from sqlite3
import Error
def main():
print("Hello World!")
if __name__ == "__main__":
main()
CREATE TABLE IF NOT EXISTS uploads(
id integer PRIMARY KEY,
file_name text NOT NULL,
file_blob text NOT NULL
);
/Users/diane/Documents/<FILE_NAME_HERE>
def main():
print("Hello World!")
file_path_name = input("Enter full file path:\n")