问题
What is the syntax to UPDATE
a BLOB
field in an existing SQLite
record, using Python? I create a 13x13 array of floats and want to Update a specific record (i.e. using a WHERE
clause) in my table with that array.
What is the proper UPDATE
syntax?
The array has the following form:
[ 4.65640926e+00 5.59250259e+00 5.28963852e+00 1.60680866e+00
-3.39492680e-01 -4.76834650e-01 -4.76834650e-01 -2.29132240e-01
1.49733067e+00 1.51563072e+00 1.49733067e+00 9.53471420e-01
-1.40306473e+00]
[ 5.28963852e+00 5.34537315e+00 5.05013466e+00 1.48362923e+00
-3.69843300e-01 -4.76834650e-01 -4.76834650e-01 -2.29132240e-01
7.60705290e-01 1.49733067e+00 9.53471420e-01 3.05504260e-01
-1.40306473e+00]
Totaling 13 rows of 13 sub-arrays.
Thank you, Bill
回答1:
The syntax for the SQL is :-
UPDATE mytable SET myblobcolumn = x'ffeedd' WHERE your_where_clause;
Where
- mytable is the table name,
- myblobcolumn is the name of the column that is to be updated,
- your_where_clause is the selection criteria,
- x'ffeedd' is the byte array value, converted to hexadecimal, that is to be used to update the column.
Obviously the above are only representations, you would have to substitute appropriate values
SQL As Understood By SQLite - UPDATE
Datatypes In SQLite Version 3
回答2:
A friend pointed me to this solution, which works nicely. The original answer was from StackOverflow, to give proper credit.
def adapt_array(arr): """ Reformat a numpy array so as to be writeable to SQLite BLOB fields Input: Numpy Array Returns: formatted Binary BLOB compatable Code Source: Python insert numpy array into sqlite3 database """ out = io.BytesIO() np.save(out, arr) out.seek(0) return sqlite3.Binary(out.read())
def convert_array(text): """ Reformat a SQLite BLOB field into the originating Numpy array Input: Numpy BLOB from SQLite Returns: numpy array Code Source: Python insert numpy array into sqlite3 database """ out = io.BytesIO(text) out.seek(0) return np.load(out)
来源:https://stackoverflow.com/questions/51217595/syntax-to-update-a-blob-field-in-an-existing-sqlite-record