使用sqlite3存储奥斯卡金像奖提名信息2

在上一步骤的基础上,使用sqlite3创建以下几个表:

  • ceremonies 存储举行时间和地点
  • movies
  • actors
  • movies_actors 用于存储movies和actors间的多对多关系

重点内容:

  • 数据库的链接/表的创建
  • commit()
  • sqlite的特性,死锁概念及原因
1.创建ceremonies

链接数据库

import sqlite3

conn=sqlite3.connect('./data/nominations.db')
schema=conn.execute("pragma table_info(nominations);").fetchall()
first_ten=conn.execute("select * from nominations limit 10; ")
for row in schema:
    print(row)
for row in first_ten:
    print(row)
(0, 'Year', 'INTEGER', 0, None, 0)
(1, 'Category', 'TEXT', 0, None, 0)
(2, 'Nominee', 'TEXT', 0, None, 0)
(3, 'Won', 'INTEGER', 0, None, 0)
(4, 'Movie', 'TEXT', 0, None, 0)
(5, 'Character', 'TEXT', 0, None, 0)
(2010, 'Actor -- Leading Role', 'Javier Bardem', 0, 'Biutiful ', 'Uxbal')
(2010, 'Actor -- Leading Role', 'Jeff Bridges', 0, 'True Grit ', 'Rooster Cogburn')
(2010, 'Actor -- Leading Role', 'Jesse Eisenberg', 0, 'The Social Network ', 'Mark Zuckerberg')
(2010, 'Actor -- Leading Role', 'Colin Firth', 1, "The King's Speech ", 'King George VI')
(2010, 'Actor -- Leading Role', 'James Franco', 0, '127 Hours ', 'Aron Ralston')
(2010, 'Actor -- Supporting Role', 'Christian Bale', 1, 'The Fighter ', 'Dicky Eklund')
(2010, 'Actor -- Supporting Role', 'John Hawkes', 0, "Winter's Bone ", 'Teardrop')
(2010, 'Actor -- Supporting Role', 'Jeremy Renner', 0, 'The Town ', 'James Coughlin')
(2010, 'Actor -- Supporting Role', 'Mark Ruffalo', 0, 'The Kids Are All Right ', 'Paul')
(2010, 'Actor -- Supporting Role', 'Geoffrey Rush', 0, "The King's Speech ", 'Lionel Logue')

创建表并插入数据:

conn.execute('''create table ceremonies(
             id integer primary key,
             Year integer,
             Host text
            );''')
years_hosts= [(2010, "Steve Martin"),
               (2009, "Hugh Jackman"),
               (2008, "Jon Stewart"),
               (2007, "Ellen DeGeneres"),
               (2006, "Jon Stewart"),
               (2005, "Chris Rock"),
               (2004, "Billy Crystal"),
               (2003, "Steve Martin"),
               (2002, "Whoopi Goldberg"),
               (2001, "Steve Martin"),
               (2000, "Billy Crystal")
            ]

#主键不用指定,系统自动设置
conn.execute("pragma foreign_keys=ON;")#每次插入操作前建议设置,防止插入不含外键的非法数据
conn.executemany("insert into ceremonies(Year,Host) values(?,?);",years_hosts)

conn.commit()#注意:每次执行修改操作应提交,close不会自动调用commit,直接关闭数据库会导致修改丢失

result=conn.execute('select * from ceremonies;').fetchall()
print(result)

返回一个由tuple组成的list:

 [(1, 2010, 'Steve Martin'), 
(2, 2009, 'Hugh Jackman'), (3, 2008, 'Jon Stewart'), 
(4, 2007, 'Ellen DeGeneres'), (5, 2006, 'Jon Stewart'), 
(6, 2005, 'Chris Rock'), (7, 2004, 'Billy Crystal'),
 (8, 2003, 'Steve Martin'), (9, 2002, 'Whoopi Goldberg'), 
(10, 2001, 'Steve Martin'), (11, 2000, 'Billy Crystal')]
2. nominations表

nominations中year列已经存在于ceremonies表中了,即冗余列,应该删除;而sqlite为保持轻量级,不允许对表结构进行修改,所以想删除某列只能通过新建的方式。

conn.execute('''create table nominations_two(
                id integer primary key,
                category text,
                nominee text,
                movie text,
                character text,
                won integer,
                ceremonies_id integer,
                foreign key(ceremonies_id) references ceremonies(id)
                );''')

useful_data=conn.execute('''select nominations.category, nominations.nominee, nominations.movie, nominations.character, nominations.won, ceremonies.id
                from nominations
                inner join ceremonies
                on nominations.year==ceremonies.year;
                ''').fetchall()
conn.executemany("insert into nominations_two(category,nominee,movie,character,won,ceremonies_id) values(?,?,?,?,?,?)",useful_data)
print(conn.execute("select * from nominations_two limit 10;").fetchall())

conn.execute('DROP table nominations;')
conn.execute('alter table nominations_two rename to nominations;')
3.创建movies/actors/movies_actors表

创建表

conn.execute("create table movies(id integer primary key,movie text);")
conn.execute("create table actors(id integer primary key,actor text);")
conn.execute('''create table movies_actors(
                id integer primary key,
                movie_id integer references movies(id),
                actor_id integer references actors(id));
                ''')

插入数据

conn.execute("insert into movies(Movie) select distinct movie from nominations;")#字段不区分大小写
conn.execute("insert into actors(Actor) select distinct Nominee from nominations;")

movie_actor_pair=conn.execute("select distinct movie,Nominee from nominations;").fetchall()
conn.executemany('''insert into movies_actors(movie_id,actor_id) 
                values((select id from movies where movie==?),(select id from actors where actor==?));'''
                ,movie_actor_pair)

conn.commit()
conn.close()

创建完成,此时我们可以通过终端或者可视化工具查看创建结果:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,457评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,837评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,696评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,183评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,057评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,105评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,520评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,211评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,482评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,574评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,353评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,897评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,489评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,683评论 2 335

推荐阅读更多精彩内容