简体   繁体   中英

How to make a SQLite database file readable as a text file?

Im using SQLAlchemy database in python and the character in database is encoded and not understandable when I open with text file. Does anyone know do I make database file character readable? Any answer would be appriciated.

Here is my database that is encoded

SQLite format 3@  %%.O}
\\Å!Ç%tablepostpostCREATE TABLE post (
    id INTEGER NOT NULL, 
    title VARCHAR(30) NOT NULL, 
    detail VARCHAR(100), 
    due DATETIME NOT NULL, 
    PRIMARY KEY (id)
)
@œ
œÕñœîU°?++A„ÅÇ„ÅÑ„ÅÜ„Åà„Åä„Åã„Åç„Åè„Åë„Åì2021-11-30 00:00:00.000000°;+#Atestings clasekasdsdasdasd2021-11-26 00:00:00.000000<   -'ASADADASDAXDSACDAASDASDASDAXAE2021-11-19 00:00:00.0000001Atest45454two pills2021-11-05 00:00:00.000000VAMedicine44 meds2021-10-27 00:00:00.000000êAtesttestone pill2021-10-25 00:05%!AAndy's pillsfour pills2021-11-03 00:00:00.0000001

I want these text to be readable

Here is my python code

from datetime import datetime, date

from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///medicine-app.db'

db = SQLAlchemy(app)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(30), nullable=False)
    detail = db.Column(db.String(100))
    due = db.Column(db.DateTime, nullable=False)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        posts = Post.query.order_by(Post.due).all()
        return render_template('index.html', posts=posts, today=date.today()) 
    else:
        title = request.form.get('title')
        detail = request.form.get('detail')
        due = request.form.get('due')

        due = datetime.strptime(due, '%Y-%m-%d')
        new_post = Post(title=title, detail=detail, due=due) #making a new post from create

        db.session.add(new_post)
        db.session.commit()

        return redirect('/')


@app.route('/create')
def create():
    return render_template('create.html')

@app.route('/detail/<int:id>')
def read(id):
    post = Post.query.get(id)
    return render_template('detail.html', post=post)

@app.route('/update/<int:id>', methods=['GET', 'POST'])
def update(id):
    post = Post.query.get(id)
    if request.method == 'GET':
        return render_template('update.html', post=post)
        #update paged
    else:
        post.title = request.form.get('title')
        post.detail = request.form.get('detail')
        request.form.get('due')
        post.due = datetime.strftime, '%Y-%m-%d'

        db.session.commit()
        return redirect('/')
        #to database
        #top page



@app.route('/delete/<int:id>')
def delete(id):
    post = Post.query.get(id)

    db.session.delete(post)
    db.session.commit()
    return redirect('/')


if __name__ == '__main__':
    app.run(debug=True)

I am also using HTML

the character in database is encoded and not understandable when I open with text file. Does anyone know do I make database file character readable?

You can't. SQLite database files are binary files, not text files. Opening them in a text editor will only produce semi-readable results because not all bytes (or sequences of bytes) represent valid characters regardless of the character encoding.

On Linux, a hexdump (hd) of a SQLite file looks like this:

$ hd gh_6927.sqlite 
00000000  53 51 4c 69 74 65 20 66  6f 72 6d 61 74 20 33 00  |SQLite format 3.|
00000010  10 00 01 01 00 40 20 20  00 00 00 2e 00 00 00 04  |.....@  ........|
00000020  00 00 00 04 00 00 00 02  00 00 00 1f 00 00 00 04  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 01 00 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 2e  |................|
00000060  00 2e 3f d9 0d 00 00 00  01 0f 95 00 0f 95 0e 96  |..?.............|
00000070  0e 96 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000e90  00 00 00 00 00 00 00 00  01 6a 19 19 01 81 45 74  |.........j....Et|
00000ea0  61 62 6c 65 74 61 62 6c  65 31 74 61 62 6c 65 31  |abletable1table1|
00000eb0  03 43 52 45 41 54 45 20  54 41 42 4c 45 20 74 61  |.CREATE TABLE ta|
00000ec0  62 6c 65 31 20 28 0a 09  69 64 20 49 4e 54 45 47  |ble1 (..id INTEG|
00000ed0  45 52 20 4e 4f 54 20 4e  55 4c 4c 2c 20 0a 09 64  |ER NOT NULL, ..d|
00000ee0  65 73 63 72 69 70 74 69  6f 6e 20 56 41 52 43 48  |escription VARCH|
00000ef0  41 52 28 32 35 30 29 2c  20 0a 09 50 52 49 4d 41  |AR(250), ..PRIMA|
00000f00  52 59 20 4b 45 59 20 28  69 64 29 0a 29 00 00 00  |RY KEY (id).)...|
00000f10  f3 17 2b 2b 01 82 09 74  61 62 6c 65 61 6c 65 6d  |..++...tablealem|
00000f20  62 69 63 5f 76 65 72 73  69 6f 6e 61 6c 65 6d 62  |bic_versionalemb|
00000f30  69 63 5f 76 65 72 73 69  6f 6e 02 43 52 45 41 54  |ic_version.CREAT|
00000f40  45 20 54 41 42 4c 45 20  61 6c 65 6d 62 69 63 5f  |E TABLE alembic_|
00000f50  76 65 72 73 69 6f 6e 20  28 0a 09 76 65 72 73 69  |version (..versi|
00000f60  6f 6e 5f 6e 75 6d 20 56  41 52 43 48 41 52 28 33  |on_num VARCHAR(3|
00000f70  32 29 20 4e 4f 54 20 4e  55 4c 4c 2c 20 0a 09 43  |2) NOT NULL, ..C|
00000f80  4f 4e 53 54 52 41 49 4e  54 20 61 6c 65 6d 62 69  |ONSTRAINT alembi|
00000f90  63 5f 76 65 72 69 01 07  17 17 17 01 81 31 74 61  |c_veri.......1ta|
00000fa0  62 6c 65 74 68 69 6e 67  74 68 69 6e 67 02 43 52  |blethingthing.CR|
00000fb0  45 41 54 45 20 54 41 42  4c 45 20 74 68 69 6e 67  |EATE TABLE thing|
00000fc0  20 28 0a 09 69 64 20 49  4e 54 45 47 45 52 20 4e  | (..id INTEGER N|
00000fd0  4f 54 20 4e 55 4c 4c 2c  20 0a 09 74 78 74 20 56  |OT NULL, ..txt V|
00000fe0  41 52 43 48 41 52 28 35  30 29 2c 20 0a 09 50 52  |ARCHAR(50), ..PR|
00000ff0  49 4d 41 52 59 20 4b 45  59 20 28 69 64 29 0a 29  |IMARY KEY (id).)|
00001000  0d 00 00 00 01 0f f3 00  0f f3 00 00 00 00 00 00  |................|
00001010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001ff0  00 00 00 0b 01 03 00 1d  6e 65 77 20 74 65 78 74  |........new text|
00002000  0d 00 00 00 00 10 00 00  00 00 00 00 00 00 00 00  |................|
00002010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00003000  00 00 00 00 00 00 00 01  00 00 00 03 00 00 00 02  |................|
00003010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00003ff0  0f 03 25 09 65 36 39 65  36 34 30 37 31 63 38 63  |..%.e69e64071c8c|
00004000

If you want the contents of a particular table in that file to be in text-readable form you can export that table to a CSV file (or similar).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM