2016-03-06 104 views
0

我下面的Flask's website什么是管道中的模式Linux的源码到Windows(瓶)

教程给大家介绍一些情况下,我在具有flaskr目录flaskapp工作的对应。 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与unneccesary部分修剪:

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() 

教程says-

Flaskr有关部分是数据库应用提供动力如前所述,更确切地说,是一个由关系数据库系统支持的应用程序。这样的系统需要一个模式,告诉他们如何存储这些信息。因此,在第一次启动服务器之前,创建该模式非常重要。

这种模式可以通过管道将schema.sql文件文件到sqlite3的命令如下创建:

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文件中的'DATABASE'是'/tmp/flaskr.db'。因此,我在我的工作目录中创建了一个空白的flaskrdb.db文件,并替换flaskr.py DATABASE值中的'tmp/flaskr.db'。但是我无法使用管道操作,因为它是针对Linux的。我如何在Windows中做到这一点? 我无法在我的电脑上的任何地方找到sqlite3.exe。

回答

0

快速的方法是使用您现有的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应用程序与这些工具一起发布,它们将与本教程中的管道示例一起使用。

相关问题