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

前端 未结 2 959
[愿得一人]
[愿得一人] 2020-12-17 14:19

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<

相关标签:
2条回答
  • 2020-12-17 14:36

    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.

    0 讨论(0)
  • 2020-12-17 14:58

    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();
    
    0 讨论(0)
提交回复
热议问题