我正在学习 Flask's website 上的教程
为了给您一些背景信息,我在一个目录 flaskapp 中工作,该目录包含 flaskr.py 文件和 schema.sql。 virtualenv 已激活。
schema.sql 是
drop table if exists entries;
create table entries (
id integer primary key autoincrement,
title text not null,
text text not null
);
去掉不必要部分的 flaskr.py:
DATABASE = '/tmp/flaskr.db'
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
教程中的相关部分说-
Flaskr is a database powered application as outlined earlier, and more precisely, an application powered by a relational database system. Such systems need a schema that tells them how to store that information. So before starting the server for the first time it’s important to create that schema.
Such a schema can be created by piping the schema.sql file into the sqlite3 command as follows:
sqlite3 /tmp/flaskr.db < schema.sql
我很确定我已经找到问题所在,因为在运行它时,错误是:
File "C:\Users\Hp1\Desktop\flaskr\flaskrapp\flaskr.py", line 19,
in connect_db
return sqlite3.connect(app.config['DATABASE'])
sqlite3.OperationalError: unable to open database file
我的 flaskr.py 文件中的“数据库”是“/tmp/flaskr.db”。所以我在我的工作目录中创建了一个空白的 flaskrdb.db 文件,并替换了 flaskr.py DATABASE 值中的“tmp/flaskr.db”。但是我无法使用管道操作,因为它是为 Linux 提供的。我如何在 Windows 中执行此操作? 我无法在我的电脑上的任何地方找到 sqlite3.exe。
最佳答案
一种快速的方法是使用您现有的 Python 安装并手动完成,仅用于您的教程。
>>> import sqlite3
>>> conn = sqlite3.connect("flaskr.db") # Replace this with the path to the flaskr.db in your working directory
>>> c = conn.cursor()
>>> c.execute("drop table if exists entries;")
<sqlite3.Cursor object at 0x0000000002C3BB90>
>>> c.execute("""create table entries (
... id integer primary key autoincrement,
... title text not null,
... text text not null
... );""")
<sqlite3.Cursor object at 0x0000000002C3BB90>
>>>
如果您需要更多地与 sqlite3 数据库交互,请考虑从 SQLite 网站安装和学习这些工具:https://www.sqlite.org/download.html -- 你需要 sqlite-tools-win32-x86-3110100.zip。 有一个与这些工具一起分发的命令行 sqlite 应用程序,它将与教程中的管道示例一起使用。
关于python - 在 Linux 到 Windows (Flask) 中将模式管道传输到 sqlite 的对应物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35833927/