前提・実現したいこと
PythonでCGIを作っています。
formから送信された情報をsqlite3で保存したいのですが、
insert文で以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
cursor.execute(sql,data)\r\nsqlite3.OperationalError: no such table: list\r\n'
tableが追加できてないようなのですが、何故だかわかりません。
該当のソースコード
Python
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3import sys, io, cgi, sqlite3, os 4sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') 5form = cgi.FieldStorage() 6connector = sqlite3.connect("lost_list.db") 7cursor = connector.cursor() 8 9# テーブル作成(初期起動時) 10sql = "select count(*) from sqlite_master where type='table' and name='list'" 11cursor.execute(sql) 12result = cursor.fetchall() 13if result[0] == 0 or result[0] == "0": 14 sql = "create table list ("\ 15 "id integer primary key,"\ 16 "image TEXT,"\ 17 "place TEXT,"\ 18 "name TEXT NOT NULL,"\ 19 "date NUMERIC,"\ 20 "finder TEXT,"\ 21 "memo TEXT)" 22 cursor.execute(sql) 23
Python
1# データベースに保存 2sql = "insert into list (image,place,name,date,finder,memo) values(?,?,?,?,?,?)" 3data = (image_name, place, name, date, finder, memo) 4cursor.execute(sql,data) 5 6connector.commit() 7cursor.close() 8connector.close()
試したこと
sqlite_masterでtableの検索を行い、結果をHTMLで出力しました。
Python
1sql = "select count(*) from sqlite_master where type='table' and name='list'" 2cursor.execute(sql) 3result = cursor.fetchall() 4 5print('Content-type: text/html; charset=UTF-8\n\n') 6print("<html>") 7print("<head>") 8print('<meta http-equiv="Content-type" content="text/html; charset=UTF-8" ></head>') 9print("<p>%s</p>" % result[0]) 10print("</body></html>")
HTML
10
補足情報(FW/ツールのバージョンなど)
Python実行のコマンドは以下のものです。
python -m http.server 8080 --cgi
ifをはずして初回のみcreate tableをいれると普通にinsertはできます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/25 04:01