Python練習(xí)之操作SQLite數(shù)據(jù)庫(kù)
前言
文章包括下幾點(diǎn):
考點(diǎn)--操作SQLite數(shù)據(jù)庫(kù):
- 創(chuàng)建SQLite數(shù)據(jù)庫(kù);
- 向表中插入記錄;
- 其他數(shù)據(jù)庫(kù)操作。
面試題:
- 1.面試題一:如何創(chuàng)建SQLite數(shù)據(jù)庫(kù)?
- 2.面試題二:如何向SQLite表中插入數(shù)據(jù)?
- 3.面試題三:如何查詢SQLite表中的數(shù)據(jù)?
1.創(chuàng)建SQLite數(shù)據(jù)庫(kù)
# coding=utf-8 # _author__ = 孤寒者 import sqlite3 import os dbPath = 'data.sqlite' if not os.path.exists(dbPath): conn = sqlite3.connect(dbPath) c = conn.cursor() c.execute('''create table persons (id int primary key not null, name text not null, age int not null, address char(100), salary real);''') conn.commit() conn.close() print('創(chuàng)建數(shù)據(jù)庫(kù)成功')
- 我們通過上述操作已經(jīng)成功創(chuàng)建了sql數(shù)據(jù)庫(kù),并在里面創(chuàng)建了一張表。
- 為了查看我們創(chuàng)建的表,我們可以用到SqliteStudio,它是一款 Sqlite數(shù)據(jù)庫(kù)可視化工具,是使用Sqlite數(shù)據(jù)庫(kù)開發(fā)應(yīng)用的必備軟件,軟件無需安裝,下載后解壓即可使用,很小巧但很了用,綠色中文版本。比起其它SQLite管理工具,我喜歡用這個(gè)。很方便易用,不用安裝的單個(gè)可執(zhí)行文件,支持中文。
2.向SQLite表中插入數(shù)據(jù)
# coding=utf-8 import sqlite3 dbPath = 'data.sqlite' conn = sqlite3.connect(dbPath) c = conn.cursor() # 首先將表中數(shù)據(jù)全部刪除 c.execute('delete from persons') # 插入數(shù)據(jù) c.execute(''' insert into persons(id,name,age,address,salary) values(1, '孤寒者', 18, 'China', 9999) ''') c.execute(''' insert into persons(id,name,age,address,salary) values(2, '小張', 55, 'China', 9) ''') conn.commit() print('insert success')
3.查詢SQLite表中的數(shù)據(jù)
# coding=utf-8 import sqlite3 dbPath = 'data.sqlite' conn = sqlite3.connect(dbPath) c = conn.cursor() persons = c.execute('select name,age,address,salary from persons order by age') # 打印查詢結(jié)果發(fā)現(xiàn)是個(gè)Cursor對(duì)象(可迭代對(duì)象) print(type(persons)) result = [] for person in persons: value = {} value['name'] = person[0] value['age'] = person[1] value['address'] = person[2] result.append(value) conn.close() print(type(result)) print(result) # 我們也可以使用前面學(xué)習(xí)的json模塊使這個(gè)list類型的result轉(zhuǎn)為字符串類型 # 網(wǎng)絡(luò)傳輸需要使用字符串類型 import json resultStr = json.dumps(result, ensure_ascii=False) print(resultStr)
總結(jié)
使用sqlite3模塊中的API可以操作SQLite數(shù)據(jù)庫(kù),該模塊是Python內(nèi)置的模塊,不需要單獨(dú)安裝。
到此這篇關(guān)于Python練習(xí)之操作SQLite數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)Python操作SQLite 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中如何使用sqlite3操作SQLite數(shù)據(jù)庫(kù)詳解
- 使用Python連接SQLite數(shù)據(jù)庫(kù)的操作步驟
- 通過python封裝SQLite3的示例代碼
- Python數(shù)據(jù)庫(kù)編程之SQLite和MySQL的實(shí)踐指南
- Python的sqlite3模塊中常用函數(shù)
- Python中SQLite數(shù)據(jù)庫(kù)的使用
- Python數(shù)據(jù)庫(kù)sqlite3圖文實(shí)例詳解
- Python使用sqlite3第三方庫(kù)讀寫SQLite數(shù)據(jù)庫(kù)的方法步驟
- python處理SQLite數(shù)據(jù)庫(kù)的方法
- SQLite5-使用Python來讀寫數(shù)據(jù)庫(kù)
- Pandas使用SQLite3實(shí)戰(zhàn)
相關(guān)文章
Python基礎(chǔ)知識(shí)點(diǎn) 初識(shí)Python.md
在本篇文章中我們給大家總結(jié)了關(guān)于Python基礎(chǔ)知識(shí)點(diǎn),通過初識(shí)Python.md的相關(guān)內(nèi)容分享給Python初學(xué)者,一起來看下吧。2019-05-05python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn)
本文主要介紹了python中CURL 和python requests的相互轉(zhuǎn)換實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python腳本實(shí)現(xiàn)定時(shí)任務(wù)的最佳方法
我們?cè)谌粘9ぷ髦?常常會(huì)用到需要周期性執(zhí)行的任務(wù),下面這篇文章主要給大家介紹了關(guān)于Python腳本實(shí)現(xiàn)定時(shí)任務(wù)的最佳方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析
這篇文章主要介紹了Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python?matplotlib?plotly繪制圖表詳解
plotly本身是個(gè)生態(tài)非常復(fù)雜的繪圖工具,它對(duì)很多編程語(yǔ)言提供接口。交互式和美觀易用應(yīng)該是?Plotly?最大的優(yōu)勢(shì),而?Matplotlib?的特點(diǎn)則是可定制化程度高,但語(yǔ)法也相對(duì)難學(xué),各有優(yōu)缺點(diǎn)。本文將通過示例詳細(xì)講解二者是如何繪制圖表的,需要的可以參考一下2022-03-03