質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

Q&A

1回答

1416閲覧

SQL のエラー sqlite3.ProgrammingError: Cannot operate on a closed cursor.について

masamuneyuya

総合スコア12

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

SQL

SQL(Structured Query Language)は、リレーショナルデータベース管理システム (RDBMS)のデータベース言語です。大きく分けて、データ定義言語(DDL)、データ操作言語(DML)、データ制御言語(DCL)の3つで構成されており、プログラム上でSQL文を生成して、RDBMSに命令を出し、RDBに必要なデータを格納できます。また、格納したデータを引き出すことも可能です。

0グッド

0クリップ

投稿2021/09/14 20:32

前提・実現したいこと

YouTubeのAPIを用いて高評価の動画情報を取得し、それをSQLのデータベースに格納し、SELECT文で検索して結果を見ることができるwebアプリを開発している。
作業中、以下のようなエラーメッセージが出てきて、行き詰まっている。

発生している問題・エラーメッセージ

sqlite3.ProgrammingError: Cannot operate on a closed cursor.

該当のソースコード

python

1from flask import Flask,render_template,request,redirect 2import sqlite3 3import httplib2 4import os 5import sys 6 7from apiclient.discovery import build 8from apiclient.errors import HttpError 9from oauth2client.client import flow_from_clientsecrets 10from oauth2client.file import Storage 11from oauth2client.tools import argparser, run_flow 12 13 14# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains 15# the OAuth 2.0 information for this application, including its client_id and 16# client_secret. You can acquire an OAuth 2.0 client ID and client secret from 17# the Google API Console at 18# https://console.developers.google.com/. 19# Please ensure that you have enabled the YouTube Data API for your project. 20# For more information about using OAuth2 to access the YouTube Data API, see: 21# https://developers.google.com/youtube/v3/guides/authentication 22# For more information about the client_secrets.json file format, see: 23# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets 24CLIENT_SECRETS_FILE = "client_secrets.json" 25 26# This variable defines a message to display if the CLIENT_SECRETS_FILE is 27# missing. 28MISSING_CLIENT_SECRETS_MESSAGE = """ 29WARNING: Please configure OAuth 2.0 30 31To make this sample run you will need to populate the client_secrets.json file 32found at: 33 34 %s 35 36with information from the API Console 37https://console.developers.google.com/ 38 39For more information about the client_secrets.json file format, please visit: 40https://developers.google.com/api-client-library/python/guide/aaa_client_secrets 41""" % os.path.abspath(os.path.join(os.path.dirname(__file__), 42 CLIENT_SECRETS_FILE)) 43 44# This OAuth 2.0 access scope allows for full read/write access to the 45# authenticated user's account. 46YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" 47YOUTUBE_API_SERVICE_NAME = "youtube" 48YOUTUBE_API_VERSION = "v3" 49 50flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, 51 message=MISSING_CLIENT_SECRETS_MESSAGE, 52 scope=YOUTUBE_READ_WRITE_SCOPE) 53 54storage = Storage("%s-oauth2.json" % sys.argv[0]) 55credentials = storage.get() 56 57if credentials is None or credentials.invalid: 58 flags = argparser.parse_args() 59 credentials = run_flow(flow, storage, flags) 60 61youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, 62 http=credentials.authorize(httplib2.Http())) 63 64# This code creates a new, private playlist in the authorized user's channel. 65videos_list_responses = youtube.videos().list( 66 myRating="like", 67 part="snippet,status" 68 ).execute() 69 70dbname = 'goodvideos.db' 71 72for count in range(1): 73 conn = sqlite3.connect(dbname) 74 cur = conn.cursor() 75 cur.execute( 76 'INSERT INTO videos(title ,thumbnail, video_id) values(?,?,?)',(videos_list_responses['items'][count]['snippet']['title'],videos_list_responses['items'][count]['snippet']['thumbnails']['default']['url'],videos_list_responses['items'][count]['id'])) 77 conn.commit() 78 79 cur.close() 80 conn.close() 81 82#Flaskオブジェクトの生成 83app = Flask(__name__) 84 85 86@app.route("/",methods=["GET", "POST"]) 87def search(): 88 if request.method == "POST": 89 keyword = request.form.get("keyword") 90 if not keyword: 91 return redirect("/") 92 conn = sqlite3.connect(dbname) 93 cur = conn.cursor() 94 95 result_videos = cur.execute("SELECT title, thumbnail, video_id FROM videos WHERE title = ?",(keyword,)) 96 cur.close() 97 conn.close() 98 return render_template("index.html",result_videos=result_videos) 99 else: 100 return render_template("search.html") 101 102 103 104 105@app.route("/index") 106def index(): 107 return render_template("index.html",result_videos=result_videos) 108 109 110 111if __name__ == "__main__": 112 app.run(debug=True) 113

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2021/09/14 21:17

PythonなりFlaskなりの質問タグはあったほうが良いと思います。
guest

回答1

0

sqlite3.ProgrammingError: Cannot operate on a closed cursor.

以下の行を修正すればどうでしょうか。

Diff

1- result_videos = cur.execute("SELECT title, thumbnail, video_id FROM videos WHERE title = ?",(keyword,)) 2 3+ cur.execute("SELECT title, thumbnail, video_id FROM videos WHERE title = ?",(keyword,)) 4+ result_videos = cur.fetchall()

投稿2021/09/16 23:05

mayu-

総合スコア335

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問