sqlite database file unreadable symbols

旧城冷巷雨未停 提交于 2021-01-29 18:51:22

问题


We have an assignment where we have to write and query from a database file directly without the use of any sqlite api functions. We were given the site https://www.sqlite.org/fileformat.html as guidance but I can't seem to get the database file to look like anything readable.

Here's a basic example of what I'm trying to do with the sqlite3 library from python

import sqlite3

con = sqlite3.connect("test.db")
cur = con.cursor()

cur.execute("PRAGMA page_size = 4096")
cur.execute("PRAGMA encoding = 'UTF-8'")
dropTable = "DROP TABLE IF EXISTS Employee"
createTable = "CREATE TABLE Employee(first int, second int, third int)"

cur.execute(dropTable)
cur.execute(createTable)

cur.execute("INSERT INTO Employee VALUES (1, 2, 3)")

con.commit()
con.close()

When I open the database file, it starts with "SQLite format 3", and then a bunch of weird symbols following it. The same thing happened when I made the databse with the actual csv file given. There's some readable parts but most of it is unreadable symbols that is in no way similar to the format specified by the website. I'm a little overwhelmed right now so I would appreciate anyone pointing me to the right direction on how I would begin fixing this mess.


回答1:


Here is an example of reading that binary file using FileIO and struct:

from io import FileIO
from struct import *


def read_header(db):
    # read the entire database header into one bytearray
    header = bytearray(100)
    db.readinto(header)

    # print out a few of the values in the header
    # any number that is more than one byte requires unpacking
    # strings require decoding
    print('header string: ' + header[0:15].decode('utf-8')) # note that this ignores the null byte at header[15]
    page_size = unpack('>h', header[16:18])[0]
    print('page_size = ' + str(page_size))
    print('write version: ' + str(header[18]))
    print('read version: ' + str(header[19]))
    print('reserved space: ' + str(header[20]))
    print('Maximum embedded payload fraction: ' + str(header[21]))
    print('Minimum embedded payload fraction: ' + str(header[22]))
    print('Leaf payload fraction: ' + str(header[23]))
    file_change_counter = unpack('>i', header[24:28])[0]
    print('File change counter: ' + str(file_change_counter))
    sqlite_version_number = unpack('>i', header[96:])[0]
    print('SQLITE_VERSION_NUMBER: ' + str(sqlite_version_number))


db = FileIO('test.db', mode='r')
read_header(db)

This only reads the database header, and ignores most of the values in the header.



来源:https://stackoverflow.com/questions/54602364/sqlite-database-file-unreadable-symbols

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!