iT邦幫忙

0

新手資料庫讀取問題

  • 分享至 

  • xImage

網頁:

  <form action="/ledger" method="POST">
    <div class="bg-gray-700 flex justify-center items-center text-xl" style="height:100px ; width: 1500px;">
      <input type="date" name="day" id="">  
      <select name="type">
        <option value="消費種類">消費種類</option>
        <option value="食物">食物</option>
        <option value="衣服">衣服</option>
        <option value="交通">交通</option>
        <option value="遊樂">遊樂</option>
        <option value="設備">設備</option>
        <option value="其他">其他</option>
        <option value="收入">收入</option>
      </select>
      <label for="" class="p-6 text-yellow-400">金額:</label><input type="text"  name="price">
      <label for="" class="p-6 text-red-500">備註:</label><input type="text" name="describe">
      <button type="submit" class="p-6 text-white" name="store" value="儲存">儲存</button>
    </div>   
  </form>
</header>
<div class="container mx-auto flex justify-center">
  <div class="" style="width: 100px;">
    <img src="img/search.jpg" alt="" style="width: 80px;" onclick="search();">
  </div>
  <table class="bg-white text-3xl text-center shadow-2xl border-2 border-black ">
    <thead class="font-bold">
      <tr >
        <td class="p-10">編號</td>
        <td class="p-10">日期</td>
        <td class="p-10">消費種類</td>
        <td class="p-10">金額</td>
        <td class="p-10">備註</td>
      </tr>
    </thead>
      <tr>
        {% for i in content %}
          <tr>
            {% for j in i %}
              <td>{{ j }}</td>
            {% endfor %}
          </tr>
        {% endfor %}
      </tr>
  </table>
</div>

連接資料庫如下:
class expenses(db.Model):
tablename = 'expenses'
id = db.Column(db.Integer,nullable=False,autoincrement=True,primary_key=True)
day = db.Column(db.Date,nullable=False,primary_key=True)
type = db.Column(db.String(100),nullable=False)
price = db.Column(db.Integer,nullable=False)
describe = db.Column(db.String(250),nullable=False)

def __init__(self,day,type,price,describe):
    self.day = day
    self.type = type
    self.price = price
    self.describe = describe

@app.route('/')
def index():
db.create_all()
with conn.cursor() as cur:
sql = """select * from expenses"""
cur.execute(sql)
index_content = cur.fetchall()
cur.close()
return render_template('index.html',content=index_content)

@app.route('/ledger',methods=["POST"])
def ledger():
#db.create_all()
if request.method =='POST':
if request.values['store'] == '儲存':

        day = request.form['day']
        type = request.form['type']
        price = request.form['price']
        describe = request.form['describe']
        data = expenses(day=day,type=type,price=price,describe=describe)
        db.session.add(data)
        db.session.commit()
with conn.cursor() as cur:
    sql = """select * from expenses"""
    cur.execute(sql)
    ledger_content = cur.fetchall()
cur.close()

return render_template('index.html',content=ledger_content)

if name == 'main':
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'

db.init_app(app)
app.debug = True
app.run()

資料庫一開始執行.py檔的時候能讀取到現有的內容,後續寫入的資料會出現在資料庫內,但沒有被cur.fetchall()抓到,導致要重新執行.py檔才會出現上次輸入的內容。
要如何更改才能在儲存的時候將資料庫的內容重新載入?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答