首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从两个列表创建sql表

从两个列表创建sql表
EN

Stack Overflow用户
提问于 2014-05-09 12:12:16
回答 2查看 1.8K关注 0票数 0

我试图创建一个sql表:我想给每个电影标题和评级一个id,并给出一个包含3个字段的表

我的密码是:

代码语言:javascript
复制
import re
import sqlite3
#conn = sqlite3.connect('imdb.db')
#c = conn.cursor()

#c.execute('''CREATE TABLE imdb
         (mov_id, Title, Rating)''')

x = open("ratings.list.txt","r")
movread = x.readlines()
x.close()



#s = raw_input('Search: ').lower()
for ns in movread:


    if 'the lord of the' in ns.lower():
        d = re.split('\s+',ns,4)
        Title = d[4]
        Rating= d[3]

        list = [Title,Rating]
        print list
        # Insert a row of data
#c.execute("INSERT INTO imdb VALUES %r;" %(tuple(Rating)))")
#       conn.commit()

我的名单是这样的:

代码语言:javascript
复制
               Movie Title                                 Rating

['The Lord of the Rings: The Return of the King (2003)\n', '8.9']
['The Lord of the Rings: The Fellowship of the Ring (2001)\n', '8.8']
['The Lord of the Rings: The Two Towers (2002)\n', '8.7']

我怎样才能把它放到SQL db中呢?

EN

Stack Overflow用户

发布于 2014-05-09 14:56:56

更新:

代码语言:javascript
复制
import re
import sqlite3
conn = sqlite3.connect('imdb.db')
c = conn.cursor()

c.execute('''CREATE TABLE imdb (mov_id ROWID, Title, Rating)''')

x = open("ratings.list.txt","r")
movread = x.readlines()
x.close()



#s = raw_input('Search: ').lower()
for ns in movread:


    if 'the lord of the' in ns.lower():
        d = re.split('\s+',ns,4)
        Title = d[4].rstrip()
        Rating= d[3]

        list = [Title,Rating]

    # Insert a row of data
        c.execute('INSERT INTO imdb (Title, Rating) values ("%s","%s")'%(list[0],list[1]))
        conn.commit()
        for row in c.execute('SELECT * FROM imdb ORDER BY Title'):
            print row

输出:

代码语言:javascript
复制
    OperationalError                          Traceback (most recent call last)
 <ipython-input-598-0de924f55a23> in <module>()
     24 
     25         # Insert a row of data
---> 26         c.execute('INSERT INTO imdb (Title, Rating) values ("%s","%s")'%     (list[0],list[1]))
     27         conn.commit()
     28         for row in c.execute('SELECT * FROM imdb ORDER BY Title'):

 OperationalError: near "5": syntax error

(None, u'The Lord of the Rings: The Return of the King (2003)', u'8.9')
(None, u'The Lord of the Rings: The Fellowship of the Ring (2001)', u'8.8')
(None, u'The Lord of the Rings: The Return of the King (2003)', u'8.9')
(None, u'The Lord of the Rings: The Fellowship of the Ring (2001)', u'8.8')
(None, u'The Lord of the Rings: The Return of the King (2003)', u'8.9')
(None, u'The Lord of the Rings: The Two Towers (2002)', u'8.7')
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23564373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档