Replace part of a string in sqlite - sqlite

I recently moved my mp3 library to my home folder, and this caused banshee to forget my file data (such as rating, ect). If I can update the file path in banshee's sqlite table, I can get restore my user-entered file data.
sqlite stores path data in a Uri field of the CoreTracks table.
Example:
Uri
file:///storage/Music/mp3/Genre/Artist/Album/Track.mp3
I'd like to change all instances to
file:///home/user/Music/mp3/Genre/Artist/Album/Track.mp3
Is there a safe way of changing just the first 15 characters of data contained in the Uri field?

Create the database backup. Then use the sqlite.exe command line shell. open your database like this:
sqlite rating.sqlite
> UPDATE CoreTracks SET Uri = 'file:///home/user' + substr(Uri, 17);
>
At the end press Ctrl-D to finish the sqlite console. I think I counted the characters correctly. Please double check if value 17 needs to be changed to something else

Related

Database name as part of file name for file group

I am scripting out our database and because we have multiple stacks of drives assigned to each file group we have the files split into multiple files per file group also to split the IO / distribute storage among drives.
This currently is in SQL Server 2017 / SSDT in VS 2019
We always gave logical names to our database that started with the name of the database like how it is default in SSMS ex "MyDbName_FileGroup" then the file name similar ex: "MyDbName_FileGorup.ndf" but we never scripted this part before, we manually set that up.
I would like to get this scripted as part of the SSDT Deployment package so it can be used to set up new DB's easily also.
Everything is great so far made scripts for each file group that will create the files, but it of course will not let me use a SQLCMD variable as part of an object name.
So trying this
ALTER DATABASE [$(DatabaseName)]
ADD FILE
(
NAME=[$(DatabaseName)_FileGroupName],
FILENAME= '$(DefaultDataPath)$(DefaultFilePrefix)_FileGroupName.mdf'
) TO FILEGROUP [MESSAGING];
GO
does not work since I can't prepend the database name to the logical name like I want it to.
Yes this is purely cosmetic to match a pattern, but how would you go about doing something like this in SSDT?

comparing two BLOB statements in SQLite using SQLiteStudio

I am currently working on a warehouse management system operated on a Raspberry Pi. Scanning a QR code should open the correct line of the database.
I read the text file/CSV file containing the QR code into my Table QR database via:
insert into QR values(readfile("C:\...\IDNumberfromQR.csv"));
this works, because the ID number appears in the database in the correct table. However, the content of the text file is read in the file type "Blob".
If I now make a table comparison via
SELECT * from warehouse management table
where PulverID=( select code from QR);
nothing appears.
However, if I enter the ID number on the computer in the table QR.code and do not have the ID read in via my file, the line I am looking for appears. So it is obviously a Data format problem.
What I already tried:
I have already set both to blob in the settings. This still did not work. The functions found in the SQLiteStudio tutorial like import(file,format,table) don't work either.
Does anyone have any idea how i can solve this problem?
Is it possible to read a CSV file as double?

How can we insert an image in sqlite database(table)?

I guess that it's valid for MySQL, however, I cannot find anything about SQLite.
Basically, I have a table which is named 'CUSTOMER'.
So I create an attribute like this:
.. Image BLOB .. after that my insert statement looks like this:
INSERT INTO CUSTOMER(1,LOAD_FILE(D:/Project/Images/X.jpg));
However, the LOAD_FILE tag is not working and I don't know how to insert an image or if we can do that.
If you're using the sqlite3 shell, the relevant function is readfile().
If you're doing this from your own program, you have to read the file into a byte array and bind it as a blob to the desired column in an insert. The exact details vary depending on language and sqlite bindings, but you shouldn't ever have to convert it to a blob literal string and embed that directly into a statement.
You can store an image as a BLOB, but you'd have to insert it as a a series of bytes using something like :-
INSERT INTO CUSTOMER (image_column, other_column) VALUES(x'0001020304........','data for the first other column');
So you'd need to convert the file into a hex string to save it.
However, it's not really recommended to store images but to rather store the path to the image and then retrieve the file when you want to display/use the image.
Saying that, SQLite can, for smaller images (say 100K), actually be more efficient 35% Faster Than The Filesystem.
You must use the cmd command line (windows) to insert the attachment. The sqllitespy (version 1.9.13) does not support de command from the program command line.
You should acess you database first with the CMD and after that;
update (your table) set (column) = readfile ('dir where the files are stored'||num||´.jpg);

Create empty sqlite db from command line

Is it possible to create an empty sqlite3 database from the command line (e.g. sqlite3 <someoption> dbname) which would create a database file for me empty of any tables so that I can access it from a different SQL editor?
Currently, when I do sqlite3 dbname, I get a sqlite prompt from which I can do CREATE TABLE ... but if I don't, when I exit, no file is created. So I am looking for a single command which would create an empty database for me without a need to create at least one table within that step.
Use the VACUUM command to create a valid empty SQLite database file, including the root database page and the database header.
sqlite3 file.db "VACUUM;"
I don't think there is a way to do that in just one statement.
In Linux I would workaround it this way:
sqlite3 aFile.db "create table aTable(field1 int); drop table aTable;"
This will automatically create the needed file with the table in it and then drop it leaving the database file without tables. That's the closest thing I know.
Anyway, I think most editors will even accept an empty file too. Give that a try.
The simple way is in bash, use command
touch file.db
This will just create a 0 size file and can be used as an empty sqlite file.
You can also call .databases when you enter the command prompt.
Or:
sqlite3 test.db ".databases"
Just create an empty file.
> test.db
Symbol ">" here means redirection.
Creating a blank db like this ( provided by mosty-mostacho ) has an advantage of being recognised as a valid SQLite db; better than an empty file.
$sqlite3 foo.db "create table t(f int); drop table t;"
Verify it by :
$file foo.db
foo.db: SQLite 3.x database, last written using SQLite version 3024000
from https://sqlite.org/cli.html
Start the sqlite3 program by typing "sqlite3" at the command prompt,
optionally followed by the name the file that holds the SQLite
database (or ZIP archive). If the named file does not exist, a new
database file with the given name will be created automatically. If no
database file is specified on the command-line, a temporary database
is created and automatically deleted when the "sqlite3" program exits.
my emphasis

Sqlite: How to cast(data as TEXT) for BLOB

I have a sqlite database from which I want to extract a column of information with the datatype BLOB. I am trying this:
SELECT cast(data as TEXT) FROM content
This is obviously not working. The output is garbled text like this:
x��Uak�0�>�8�0Ff;I�.��.i׮%�A��s�M
The data in the content column is mostly text, but may also have images (which I recognized could cause a problem if I cast as TEXT). I simply want to extract that data into a usable format. Any ideas?
You can use
SELECT hex(data) FROM content
or
SELECT quote(data) FROM content
The first will return a hex string (ABCD), the second quoted as an SQL literal (X'ABCD').
Note that there's (currently) no way of converting hexadecimal column information back to a BLOB in SQLite. You will have to use C/Perl/Python/… bindings to convert and import those.
You can write some simple script which will save all blobs from your database into files. Later, you can take a look at these files and decide what to do with them.
For example, this Perl script will create lots of files in current directory which will contain your data blob fields. Simply adjust SELECT statement to limit fetched rows as you need:
use DBI;
my $dbh = DBI->connect("dbi:SQLite:mysqlite.db")
or die DBI::errstr();
my $sth = $dbh->prepare(qq{
SELECT id, data FROM content
});
$sth->execute();
while (my $row = $sth->fetchrow_hashref()) {
# Create file with name of $row->{id}:
open FILE, ">", "$row->{id}";
# Save blob data into this file:
print FILE $row->{data};
close FILE;
}
$dbh->disconnect();

Resources