使用sqlite3存儲奧斯卡金像獎提名信息2

在上一步驟的基礎(chǔ)上缭召,使用sqlite3創(chuàng)建以下幾個表:

  • ceremonies 存儲舉行時間和地點
  • movies
  • actors
  • movies_actors 用于存儲movies和actors間的多對多關(guān)系

重點內(nèi)容:

  • 數(shù)據(jù)庫的鏈接/表的創(chuàng)建
  • commit()
  • sqlite的特性,死鎖概念及原因
1.創(chuàng)建ceremonies

鏈接數(shù)據(jù)庫

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')

創(chuàng)建表并插入數(shù)據(jù):

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")
            ]

#主鍵不用指定,系統(tǒng)自動設(shè)置
conn.execute("pragma foreign_keys=ON;")#每次插入操作前建議設(shè)置,防止插入不含外鍵的非法數(shù)據(jù)
conn.executemany("insert into ceremonies(Year,Host) values(?,?);",years_hosts)

conn.commit()#注意:每次執(zhí)行修改操作應(yīng)提交哥桥,close不會自動調(diào)用commit蚓再,直接關(guān)閉數(shù)據(jù)庫會導(dǎo)致修改丟失

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列已經(jīng)存在于ceremonies表中了站粟,即冗余列浦妄,應(yīng)該刪除呢灶;而sqlite為保持輕量級吴超,不允許對表結(jié)構(gòu)進行修改,所以想刪除某列只能通過新建的方式鸯乃。

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.創(chuàng)建movies/actors/movies_actors表

創(chuàng)建表

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));
                ''')

插入數(shù)據(jù)

conn.execute("insert into movies(Movie) select distinct movie from nominations;")#字段不區(qū)分大小寫
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()

創(chuàng)建完成鲸阻,此時我們可以通過終端或者可視化工具查看創(chuàng)建結(jié)果:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市飒责,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌仆潮,老刑警劉巖宏蛉,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異性置,居然都是意外死亡拾并,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門鹏浅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嗅义,“玉大人奥吩,你說我怎么就攤上這事深纲〕强矗” “怎么了刨啸?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵灭抑,是天一觀的道長蔬咬。 經(jīng)常有香客問我拘央,道長疚俱,這世上最難降的妖魔是什么式塌? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任博敬,我火速辦了婚禮,結(jié)果婚禮上峰尝,老公的妹妹穿的比我還像新娘偏窝。我一直安慰自己,他們只是感情好武学,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布祭往。 她就那樣靜靜地躺著,像睡著了一般火窒。 火紅的嫁衣襯著肌膚如雪链沼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天沛鸵,我揣著相機與錄音括勺,去河邊找鬼缆八。 笑死,一個胖子當(dāng)著我的面吹牛疾捍,可吹牛的內(nèi)容都是我干的奈辰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼乱豆,長吁一口氣:“原來是場噩夢啊……” “哼奖恰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起宛裕,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤瑟啃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后揩尸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蛹屿,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年岩榆,在試婚紗的時候發(fā)現(xiàn)自己被綠了错负。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡勇边,死狀恐怖犹撒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情粒褒,我是刑警寧澤识颊,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站奕坟,受9級特大地震影響谊囚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜执赡,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一镰踏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沙合,春花似錦奠伪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至究履,卻和暖如春滤否,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背最仑。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工藐俺, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留炊甲,地道東北人。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓欲芹,卻偏偏與公主長得像卿啡,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子菱父,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容