如何使SQLite数据库文件可作为文本文件读取?

m528fe3b  于 12个月前  发布在  SQLite
关注(0)|答案(2)|浏览(152)

我在python中使用SQLAlchemy数据库,数据库中的字符是编码的,当我用文本文件打开时无法理解。有人知道我是否可以使数据库文件字符可读吗?任何答案都将是appriciated。
这是我的数据库,

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

字符串
我希望这些文字是可读的
下面是我的Python代码

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)


我也用HTML

hsvhsicv

hsvhsicv1#

数据库中的字符是编码的,当我打开文本文件时无法理解。2有人知道我可以让数据库文件的字符可读吗?
不能。SQLite数据库文件是二进制文件,而不是文本文件。在文本编辑器中打开它们只会产生半可读的结果,因为无论字符编码如何,并非所有字节(或字节序列)都表示有效字符。
在Linux上,SQLite文件的hexdump(hd)看起来像这样:

$ 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

字符串
如果您希望该文件中特定表格的 * 内容 * 为文本可读形式,则可以将该表格导出为CSV文件(或类似文件)。

nkcskrwz

nkcskrwz2#

下面是一个快速而肮脏的shell脚本,用于将每个表导出到table.csv,最后导出到dbfile.sql整个模式

dbname=$1;tables=$(sqlite3 $dbname  ".tables");for table in $tables ; do echo $table ; sqlite3 -header -csv -separator "\t" $dbname  "select * from $table;">$table.csv;done;sqlite3 $dbname '.schema' > $dbname-schema.sql

字符串
复制该代码,因为它是在文件forexample sqlite2csv.sh,然后运行

sh sqlite2csv.sh dbname.sqlite


你会得到所有的表CSV和模式文件。尝试它,并给给予我反馈,如果有任何问题。

相关问题