2016-05-01 16 views
1

私はあなたの助言と助けが必要です。Python SQLite3 insertはエラーを返しますが、テーブルにデータはありません

地域の名前と特定のウェブサイトの対応する地域のリンクを解析するコードを作成しています。その後、地域の名前とリンクをデータベース(sqlite3)に保存します。データベースが作成されましたが、テーブルが作成されましたが、データをテーブルに挿入できませんでした。私はいくつかの試行錯誤を試みたが、誰も働かなかった。したがって、私はこのスレッドを作った。

''' 
usage python capstonePy.py http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs 

URL: http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs 

Official supporters URL pattern: 
http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs/[region] 
''' 

from sys import argv 
from os.path import exists 
from BeautifulSoup import * 
import urllib 
import re 
import sqlite3 

class FindSupporters: 

    def __init__(self, *args, **kwargs): 
     #parsing the url from the command line  
     url = argv[1] 

     #make a new database 
     cur = new_db('liverpudlian.sqlite3') 

     #open and read the url  
     fhand = open_and_read(url) 

     #print how many characters have been retrieved 
     suc_ret(len(fhand)) 

     #make a list of links (href) 
     linklst = find_link(fhand) 

     #make a list of supporters regions 
     offsuplinklst = fans_link(linklst) 

     #make a new table and insert the data 
     officialsup_table(cur, offsuplinklst, 'liverpudlian.sqlite3') 

     sqlite3.connect('liverpudlian.sqlite3').close()  

def new_db(name): 
    conn = sqlite3.connect(name) 
    cur = conn.cursor() 
    return cur 

def open_and_read(url): 
    try:  
     fhand = urllib.urlopen(url).read() 
    except: 
     print '\n' 
     print "+------------------------------------------------------------------------------+" 
     print "|\t\t\t\tError: URL not found.\t\t\t\t|" 
     print "+------------------------------------------------------------------------------+" 
     print '\n'  
     quit() 
    return fhand 

def suc_ret(length): 
    print '\n' 
    print "+------------------------------------------------------------------------------+" 
    print "|\t\t", length, "characters have been successfully retrieved\t\t|" 
    print "+------------------------------------------------------------------------------+" 
    print '\n' 

def find_link(fhand): 
    links = [] 
    tags = [] 
    soup = BeautifulSoup(fhand) 
    tags = soup('a') 
    for tag in tags: 
     tag = tag.get('href',None) 
     if tag is not None : 
      links.append(tag) 
    return links 

def fans_link(linklst): 
    offsuplinklst = [] 
    for link in linklst: 
     link = str(link)   
     link = link.rstrip() 
     fans = re.findall('.*fans/.+clubs/(.+)', link) 
     if len(fans) > 0: 
      offsuplinklst.append(fans[0]) 
    return offsuplinklst 

def officialsup_table(cur, offsuplinklst, name): 
    cur.execute(''' 
    create table if not exists OfficialSup 
    (ID integer primary key, 
    Region text unique, 
    Link text unique, 
    Retrieved integer)''') 
    cur.execute('select Region from OfficialSup where Retrieved = 1 limit 1') 
    try : 
     cur.fetchone()[0]' 
    except :   
     for i in range(len(offsuplinklst)): 
      reg = offsuplinklst[i] 
      link = 'http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs/'+offsuplinklst[i]    
      cur.execute('insert into OfficialSup (Region, Link, Retrieved) values (?, ?, 1)', (reg, link)) 
    sqlite3.connect(name).commit() 

FindSupporters() 

officialsup_table方法ではおそらくエラー:

は、ここに私のコードです。それにもかかわらず、私の試みは良い結果を返しませんでした。

ありがとうございます! 。あなたのカーソルが作成されたのと同じ接続インスタンスを使用してコミットする必要がありconncurの両方を返すようにnew_dbを改善

よろしく、 アーノルドA.

答えて

1

def new_db(name): 
    conn = sqlite3.connect(name) 
    cur = conn.cursor() 
    return conn, cur 

あなたがする必要があるでしょう関数の結果を今異なる方法で読む:

class FindSupporters: 

    def __init__(self, *args, **kwargs): 
     #parsing the url from the command line  
     url = argv[1] 

     #make a new database 
     conn, cur = new_db('liverpudlian.sqlite3') 

     # ... 

ctionのofficialsup_table関数にオブジェクトにも及びcommit()を呼び出す:

``近い()のために同じ
def officialsup_table(conn, cur, offsuplinklst, name): 
    cur.execute(''' 
    create table if not exists OfficialSup 
    (ID integer primary key, 
    Region text unique, 
    Link text unique, 
    Retrieved integer)''') 
    conn.commit() 

    cur.execute('select Region from OfficialSup where Retrieved = 1 limit 1') 
    try : 
     cur.fetchone()[0] 
    except :   
     for i in range(len(offsuplinklst)): 
      reg = offsuplinklst[i] 
      link = 'http://www.liverpoolfc.com/fans/lfc-official-supporters-clubs/'+offsuplinklst[i]    
      cur.execute('insert into OfficialSup (Region, Link, Retrieved) values (?, ?, 1)', (reg, link)) 
    conn.commit() 
+0

。 –

+0

@alecxeあなたの答えには大変感謝しています。試してみて、それはとてもうまくいった!ちょうどもう1つの質問ですが、なぜforループの後など、1回ではなく2回()をコミットする必要がありますか? – arnold

+0

@arnoldあなたは1回のコミットでそれを行うこともできます。私はテーブル作成を安定させてからさらに進めることにしました。ありがとう。 – alecxe

関連する問題