2016-01-11 56 views
2

我使用this tutorial來學習燒瓶。在第二段它說,使用這個命令:從Windows 8中的.sql文件創建sqlite3數據庫

sqlite3 /tmp/flaskr.db < schema.sql 

但我使用的是Windows 8,我所能做的,而不是該命令的是什麼?這是我的SQL代碼:

drop table if exists entries; 
create table entries (
    id integer primary key autoincrement, 
    title text not null, 
    text text not null 
); 
+0

我認爲這是sqlite3.exe。所以你可以用Cmd中的參數來調用它。但也許有點不同 – etalon11

+0

不,我不能在sqlite3中使用此命令。我測試了它。你能告訴它我怎樣才能從cmd調用它? –

回答

1

只要保持以下教程通過添加init_db方法和運行下面的python腳本:

# all the imports 
import sqlite3 
from flask import Flask 
from contextlib import closing 

# configuration 
DATABASE = './flaskr.db' 
DEBUG = True 


# create our little application :) 
app = Flask(__name__) 
app.config.from_object(__name__) 

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


def connect_db(): 
    return sqlite3.connect(app.config['DATABASE']) 

if __name__ == '__main__': 
    init_db() 
    #app.run() 

,使之簡單,數據庫文件flaskr.db會被創建在當前目錄和schema.sql應該也在那裏...

+0

非常感謝你,這個腳本爲我工作。但讓我問2個問題:我可以將SECRET_KEY和USERNAME和PASSWORD添加到此腳本中?此腳本和使用.read schema.sql有什麼區別,並將其保存爲sqlite3.exe中的flaskr.db? –

+0

是的,您可以在腳本中添加其他字段,例如SECRET_KEY,USERNAME,它與運行sqlite3 flaskr.db

相關問題