2017-03-08 16 views
2

PythonでDo not Repeat Yourselfの概念を適用しようとしています。データベース内の別の関数で関数内の変数を呼び出す

import sqlite3 



# Start connection and create cursor 
def startdb(): 
    # 1. Create connection 
    conn = sqlite3.connect("books.db") 
    # 2. Create a cursor object 
    cur = conn.cursor() 

# Commit and close db 
def closedb(): 
    # 4. Commit changes 
    conn.commit() 
    # 5. Close connections 
    conn.close() 

# Connect python to db 
def connect(): 
    startdb() 
    # 3. Create table if does not exist 
    cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text, year integer, isbn integer)") 
    closedb() 

# Insert data to db 
def insert(title,author,year,isbn): 
    startdb() 
    # SQL queries to insert 
    cur.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title,author,year,isbn)) 
    closedb() 

# View all datas 
def view(): 
    startdb() 
    cur.execute("SELECT * FROM book") 
    rows=cur.fetchall() 
    conn.close() 
    return rows 



connect() 
insert("The sea","John Tablet",1983,913123132) 
print(view()) 

と明らかに私は私の理解に基づい名エラー

Traceback (most recent call last): 
    File "backend.py", line 45, in <module> 
    connect() 
    File "backend.py", line 25, in connect 
    cur.execute("CREATE TABLE IF NOT EXISTS b 
ook (id INTEGER PRIMARY KEY, title text, auth 
or text, isbn integer)") 

NameError: name 'cur' is not defined 

を得、これは、startdb()機能は、私が検索した内容に基づいて可変conncur

に通過させないことを意味し__init__の機能を持つクラスを使用する必要があります。startdb()closedb()の機能を使用するより良いソリューションはありますか?

+0

を作成するには、SQL年の欄を忘れているあなたのsqlite3のクエリ、中にミスを犯しました。 '__init__'関数は、dbを起動し、現在のインスタンスの属性として' cur'を記録するのに役立ちます。あなたがこのすべてに慣れていないなら、最初にクラス/ OOPSを読むことをお勧めします。 –

+0

クラスのための良いユースケースですが、これを行うためにクラスは必要ありません。経験のために、私はこれを関数を使って実装し、 'curr'オブジェクトと' conn'オブジェクトを渡すか、グローバルな 'curr'オブジェクトと' conn'オブジェクトを使用します。そして、あなたがその作業をした後、コードをクラスに屈折させます。 –

+0

@ juanpa.arrivillaga uは 'global curr = conn.cursor'のようなものを作成することを意味しますか? –

答えて

1

@ juanpa.arrivillagaで述べたように、グローバルステートメントが必要です。 あなたははい、あなたがクラスに、これらの機能を配置する必要があり、テーブル、クエリ

import sqlite3 



# Start connection and create cursor 
def startdb(): 
    global conn, cur 
    # 1. Create connection 
    conn = sqlite3.connect("books.db") 
    # 2. Create a cursor object 
    cur = conn.cursor() 

# Commit and close db 
def closedb(): 
    # 4. Commit changes 
    conn.commit() 
    # 5. Close connections 
    conn.close() 

# Connect python to db 
def connect(): 
    startdb() 
    # 3. Create table if does not exist 
    cur.execute("CREATE TABLE IF NOT EXISTS book (id INTEGER PRIMARY KEY, title text, author text,year integer, isbn integer)") 
    closedb() 

# Insert data to db 
def insert(title,author,year,isbn): 
    startdb() 
    # SQL queries to insert 
    cur.execute("INSERT INTO book VALUES (NULL,?,?,?,?)",(title,author,year,isbn)) 
    closedb() 

# View all datas 
def view(): 
    startdb() 
    cur.execute("SELECT * FROM book") 
    rows=cur.fetchall() 
    conn.close() 
    return rows 



connect() 
insert("The sea","John Tablet",1983,913123132) 
print(view()) 
関連する問題