




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第十一章數(shù)據(jù)庫(kù)操作1.答:importsqlite3#創(chuàng)建數(shù)據(jù)庫(kù)文件Businessdbconn=sqlite3.connect('Businessdb.db')cursor=conn.cursor()#創(chuàng)建Info表cursor.execute('''CREATETABLEIFNOTEXISTSInfo(product_idTEXT,product_nameTEXT,unit_priceINTEGER)''')#創(chuàng)建Customer表cursor.execute('''CREATETABLEIFNOTEXISTSCustomer(customer_idTEXT,customer_nameTEXT,product_idTEXT)''')#輸出Info表的所有內(nèi)容defshow_info_table():cursor.execute('SELECT*FROMInfo')info_records=cursor.fetchall()forrecordininfo_records:print(f'ProductID:{record[0]},ProductName:{record[1]},UnitPrice:{record[2]}')#添加商品購(gòu)買信息到Customer表defadd_customer_record(customer_id,customer_name,product_id):cursor.execute('INSERTINTOCustomerVALUES(?,?,?)',(customer_id,customer_name,product_id))mit()print("Customerrecordaddedsuccessfully.")#查詢顧客編號(hào)的消費(fèi)金額defshow_customer_total_amount(customer_id):cursor.execute('SELECTcustomer_id,SUM(unit_price)AStotal_amountFROMCustomer''JOINInfoONCduct_id=Iduct_id''WHEREcustomer_id=?GROUPBYcustomer_id',(customer_id,))result=cursor.fetchone()ifresult:print(f'CustomerID:{result[0]},TotalAmount:{result[1]}')else:print("CustomerIDnotfound.")#示例數(shù)據(jù):添加一些商品信息cursor.executemany('INSERTINTOInfoVALUES(?,?,?)',[('130207','ProductA',100),('130208','ProductB',200),('130209','ProductC',150)])mit()#輸出Info表的所有內(nèi)容show_info_table()#添加商品購(gòu)買信息whileTrue:customer_id=input("請(qǐng)輸入顧客編號(hào)(輸入0退出程序):")ifcustomer_id=='0':breakcustomer_name=input("請(qǐng)輸入顧客姓名:")product_id=input("請(qǐng)輸入購(gòu)買商品編號(hào):")add_customer_record(customer_id,customer_name,product_id)#查詢顧客編號(hào)的消費(fèi)金額customer_id_input=input("請(qǐng)輸入顧客編號(hào)查詢消費(fèi)金額:")show_customer_total_amount(customer_id_input)#關(guān)閉數(shù)據(jù)庫(kù)連接conn.close()2.答:importsqlite3#創(chuàng)建數(shù)據(jù)庫(kù)文件flmdbconn=sqlite3.connect('flmdb.db')cursor=conn.cursor()#創(chuàng)建“熱映電影”表cursor.execute('''CREATETABLEIFNOTEXISTShot_movies(movie_nameTEXT,movie_typeTEXT,movie_regionTEXT)''')#創(chuàng)建“排片”表cursor.execute('''CREATETABLEIFNOTEXISTSschedules(movie_nameTEXT,theaterTEXT,ticket_priceINTEGER)''')#輸出“熱映電影”表的所有內(nèi)容defshow_hot_movies():cursor.execute('SELECT*FROMhot_movies')movies=cursor.fetchall()formovieinmovies:print(f'MovieName:{movie[0]},Type:{movie[1]},Region:{movie[2]}')#添加排片信息到“排片”表defadd_schedule(movie_name,theater,ticket_price):cursor.execute('INSERTINTOschedulesVALUES(?,?,?)',(movie_name,theater,ticket_price))mit()print("Scheduleaddedsuccessfully.")#查詢電影類型的排片信息defshow_schedule_by_type(movie_type):cursor.execute('SELECTmovie_name,theater,ticket_priceFROMschedulesWHEREmovie_nameIN''(SELECTmovie_nameFROMhot_moviesWHEREmovie_type=?)',(movie_type,))schedules=cursor.fetchall()forscheduleinschedules:print(f'MovieName:{schedule[0]},Theater:{schedule[1]},TicketPrice:{schedule[2]}')#示例數(shù)據(jù):添加一些熱映電影和排片信息cursor.executemany('INSERTINTOhot_moviesVALUES(?,?,?)',[('MovieA','Action','USA'),('MovieB','Comedy','China'),('MovieC','Drama','UK')])cursor.executemany('INSERTINTOschedulesVALUES(?,?,?)',[('MovieA','Theater1',20),('MovieA','Theater2',25),('MovieB','Theater3',18)])mit()#輸出“熱映電影”表的所有內(nèi)容show_hot_movies()#添加排片信息add_schedule('MovieB','Theater4',22)#查詢電影類型的排片信息show_schedule_by_type('Action')#關(guān)閉數(shù)據(jù)庫(kù)連接conn.close()3.答:importsqlite3#創(chuàng)建數(shù)據(jù)庫(kù)文件Dormdbconn=sqlite3.connect('Dormdb.db')cursor=conn.cursor()#創(chuàng)建"Dorm"表cursor.execute('''CREATETABLEIFNOTEXISTSDorm(dorm_numberTEXT,phoneTEXT,accommodation_feeINTEGER,bed_countINTEGER)''')#創(chuàng)建"Student"表cursor.execute('''CREATETABLEIFNOTEXISTSStudent(student_idTEXT,student_nameTEXT,dorm_numberTEXT)''')#輸出"Dorm"表的所有內(nèi)容defshow_dorms():cursor.execute('SELECT*FROMDorm')dorm_records=cursor.fetchall()forrecordindorm_records:print(f'DormNumber:{record[0]},Phone:{record[1]},AccommodationFee:{record[2]},BedCount:{record[3]}')#添加學(xué)生信息到"Student"表defadd_student(student_id,student_name,dorm_number):cursor.execute('INSERTINTOStudentVALUES(?,?,?)',(student_id,student_name,dorm_number))mit()print("Studentaddedsuccessfully.")#查詢學(xué)生所住的宿舍信息defshow_student_dorm_info(student_id):cursor.execute('SELECTdorm_number,phone,accommodation_feeFROMDormWHEREdorm_number=(SELECTdorm_numberFROMStudentWHEREstudent_id=?)',(student_id,))result=cursor.fetchone()ifresult:print(f'DormNumber:{result[0]},Phone:{result[1]},AccommodationFee:{result[2]}')else:print("Studentnotfound.")#示例數(shù)據(jù):添加一些宿舍信息cursor.executemany('INSERTINTODormVALUES(?,?,?,?)',[('Dorm1','123456',500,4),
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025廣州市購(gòu)銷合同范本(定版)
- 幼兒早期學(xué)習(xí)支持知到課后答案智慧樹章節(jié)測(cè)試答案2025年春山東省文登師范學(xué)校
- 2025合同法的新發(fā)展與實(shí)踐應(yīng)用
- 2025年家庭裝修工程合同范本
- 2025年設(shè)備的租賃合同范本
- 2024年鄭州市保安服務(wù)集團(tuán)有限公司招聘真題
- 總復(fù)習(xí) 數(shù)的運(yùn)算第4課時(shí) 教案2024-2025學(xué)年數(shù)學(xué)六年級(jí)下冊(cè)-北師大版
- 2024年邵陽(yáng)市民政局所屬事業(yè)單位招聘工作人員真題
- 2024年衢州市衢江區(qū)綜合事業(yè)單位招聘真題
- 2024年樂(lè)山市五通橋區(qū)人民醫(yī)院中醫(yī)醫(yī)院招聘真題
- 2024年初級(jí)會(huì)計(jì)實(shí)務(wù)考試真題及答案(5套)
- 2025年學(xué)校食堂防火應(yīng)急演練工作實(shí)施方案
- 2025屆廣東省高三第一次調(diào)研考試語(yǔ)文試題講評(píng)課件
- 2025年4月自考00152組織行為學(xué)押題及答案
- 山東省名校聯(lián)盟2024-2025學(xué)年高一3月校際聯(lián)考英語(yǔ)試題(原卷版+解析版)
- 數(shù)據(jù)庫(kù)應(yīng)用技術(shù)-第三次形考作業(yè)(第10章~第11章)-國(guó)開-參考資料
- 2024年四川省資陽(yáng)市中考物理試題【含答案、解析】
- 元朝的建立與統(tǒng)一課件 2024-2025學(xué)年統(tǒng)編版七年級(jí)歷史下冊(cè)
- 8個(gè)事故案例13個(gè)警示視頻文字完善篇(礦山局迎檢資料)
- 國(guó)旗下講話第三周校長(zhǎng)講話稿:以習(xí)慣鑄舟楫 以品格揚(yáng)云帆-讓成長(zhǎng)在堅(jiān)守中綻放華章
- 三門峽水庫(kù)實(shí)習(xí)報(bào)告
評(píng)論
0/150
提交評(píng)論