Add file to blob fileld in sqlite db - sql

How can I add image file, for example, with name '001.png' to sqlite3 database using its command prompt?
sqlite3> INSERT INTO test (id, name, blob_field) VALUES (NULL, 'Bob', ?????);

Depending on the OS you're using, you could convert the file into a hexdump and use that to construct an SQL command with a blob literal:
(printf "INSERT INTO test(id, name, blob_field) VALUES(NULL, 'Bob', 0x" ; \
hexdump -v -e '/1 "%02x"' 001.png ; printf ");" ) | sqlite3 my.db

Use hex literal like 0x123456789abcdef
so
INSERT INTO test (id, name, blob_field) VALUES (NULL, 'Bob', 0xB0B1);

Related

How to manually insert a BLOB into a SQLite database

I want to know how to manually insert a BLOB into my SQLite database. By manually I mean, without using a driver feature that will complete the command like setBytes:
Connection con = DriverManager.getConnection("jdbc:sqlite:database.db");
PreparedStatement stmt = con.prepareStatement("INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, ?)";
stmt.setBytes(1, new byte[] {0x37, 0xe7, 0x9f});
stmt.executeUpdate();
Is it possible to use a command like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, 37e79f);
or like that:
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, BLOB(37, e7, 9f));
I don't mind if the command includes base64 data or raw data, I don't want to specifically use hexadecimal.
You can use the following :-
INSERT OR REPLACE INTO test (id, aBlobColumn) VALUES (0, x'37e79f');
However, the value has to be a hex string for it to be a BLOB.

Postgres insert into a table

I have a SQL script like this which I run from the command line using psql:
insert into "A"."B" values
(1, 'name=a', 'a#example.com', 'K')
How do I convert it into INSERT command inside a database?
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
Also what does "A"."B" do? I read somewhere that double quotes are needed when table name has Capitals. I seem to get an error with that when I run commands inside the database.
You said that your database name was DB and your table name was B.
You can simply use the table name alone:
INSERT INTO "B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
If you want to include the database name, then use:
INSERT INTO "DB"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');
The double quotes are only required when the name of any entity (e.g. table, column, etc...) is a reserved word.
You can use this query where A is schema and B is table name.
INSERT INTO "A"."B" (first_column, second_c, third_c, fourth_1)
VALUES ('2', 'name=a', 'a#example.com.com', 'K');

How to insert regex with special characters like “&,?.#:;” in oracle database?

I have a regex value that I want to insert in oracle database table column but I have some problems that the value doesn't inserted correctly in the database
this is the value that I want to insert :
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)?]', 1);
the result of this insert is :
valid_value
+--------------------------------------+
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)]
I lose the "?" character can some one help me on this how to insert regex value in a table column.
I start the script using a batch file
set DB_CREATE_ROOT="%~dp0"
set SQL_INIT_CONF_DIR=%DB_CREATE_ROOT%\Scripts\configuration\
for /r %SQL_INIT_CONF_DIR% %%F in (*init.sql) do (
ECHO %DATE% %TIME% "%%F" >> %LOG_NAME%
sqlplus -L Test_APP/welkom#//localhost:1521/xe #"%%F" >> %LOG_NAME% 2>&1
)
thanks in advance
You can use CHR function for each of the special characters if you find difficulties with direct inserts.
Thanks
For the records, none of the characters mentioned have a special meaning inside SQL strings thus they don't need any special treatment:
SQL> SELECT '&,?.#:;' AS are_we_special
2 FROM DUAL;
ARE_WE_
-------
&,?.#:;
(online demo)
... being & the only possible exception (“SET DEFINE OFF” in Oracle Database) and only in the context of SQL*Plus and SQL Developer—in which case you'd be getting a Enter value for xxxxx prompt.
Whatever problem the OP had, CHR() is just a workaround for his specific undisclosed issue.
This may help you
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+)'?']', 1);
Try this:
INSERT INTO valid_value VALUES (9, 14, 'REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]', 1);
See demo:
In SQLPLUS:
SQL> /
COL1
---------------------------------------------
REGEX[[A-Z.,-\s]+(,\s[A-Za-z.,-\s]+) ?]
Now question is which client you are using. In SQLPLUS, its working as shown above.

how to execute multiple insert statements from HANA at once

I am having a file file.txt which consists of millions of insert commands like below:
"INSERT INTO T (id, serial) VALUES (0, ARRAY (1) );"
"INSERT INTO T (id, serial) VALUES (1, ARRAY (1, 2) );"
"INSERT INTO T (id, serial) VALUES (2, ARRAY (1, 2, 3, 4, 5) );"
"INSERT INTO T (id, serial) VALUES (3, ARRAY (1, 2) ); "
"INSERT INTO T (id, serial) VALUES (4, ARRAY (1, 2, 3) );"
.....
.....
.....
I want to execute them in HANA using hdbsql or Studio. But how to execute them from the file? Any help is appreciated.
Ideally, the commands shouldn't be enclosed in quotation marks.
Then you can either just run the file as-is in HANA studio (only a good idea if your file is not actually MILLIONS of files, since HANA studio parses the SQL commands first and likely will stall, when there are too many), or you run it via hdbsql with the -I option:
hdbsql -U <your secure store logon key here> -I insert.sql
Thinking about it, the latter method would be the one I would likely go for.

sqlite - Insert data into blob

I'm trying to insert binary data into a blob using SQLite3's shell, which means regular SQL statements. Here's my table:
CREATE TABLE MYTABLE
(ID INTEGER,
BINDATA BLOB NOT NULL,
SOMEFK INTEGER REFERENCES OTHERTABLE(ID) NOT NULL,
PRIMARY KEY(ID)
);
And this is the kind of insert statement I'm trying:
INSERT INTO MYTABLE (BINDATA, SOMEFK)
VALUES (__READBINDATA('/tmp/somefile'), 1);
With __READBINDATA(file) being the function I am looking for. Is that possible?
There is no built-in or shell function to read a file into a blob.
However, with the help of the hexdump tool, it's possible to transform a file's contents into a blob literal:
echo "insert into mytable(bindata, somefk) " \
"values(x'"$(hexdump -v -e '1/1 "%02x"' /tmp/somefile)"', 1);"
This command can then be piped into the sqlite3 shell.