Search code examples
pythonsqlitebytefastapifile-conversion

SQLite byte to SQLite File


I have a .sqlite file that was converted into a byte format, I would like to know how to convert it to a format where I can manipulate the data inside of it, as well as read.

The code is in python and the database is in sqlite.

Anything is helpful! Would like to have a direction to go.

b'SQLite format 3\x00\x10\x00\x01\x01\x00@  \x00\x04\xa4\xb2\x00\x00\x0f\xed\x00\x00\x04~\x00\x00\x00\x18\x00\x00\x002\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\xa4\xb2\x00.S`\x05\x00\x00\x00\x0b\x0f\xc9\x00\x00\x00\x08F\x0f\xfb\x0f\xf6\x0f\xf1\x0f\xec\x0f\xe7\x0f\xe2\x0f\xdd\x0f\xd8\x0f\xd3\x0f\xce\x0f\xc9\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x

Solution

  • If that's just raw bytes you should be able to save it to a file:

    1. Prepare some data
    $ sqlite3 test.sqlite
    SQLite version 3.36.0 2021-06-18 18:36:39
    Enter ".help" for usage hints.
    sqlite> CREATE TABLE t1 (c1 INT);
    sqlite> INSERT INTO t1 VALUES (1);
    sqlite> SELECT * FROM t1;
    1
    sqlite> .quit
    
    2. Create a new DB file from bytes:
    >>> db = bytes(open("test.sqlite", "rb").read())
    >>> db[:20]
    b'SQLite format 3\x00\x10\x00\x01\x01'
    >>> open("new.sqlite", "wb").write(db)
    8192
    
    3. Test
    >>> import sqlite3
    >>> con = sqlite3.connect("new.sqlite")
    >>> cur = con.cursor()
    >>> for row in cur.execute("SELECT * FROM t1"): print(row)
    ... 
    (1,)