Python練習(xí)之操作SQLite數(shù)據(jù)庫
前言
文章包括下幾點:
考點--操作SQLite數(shù)據(jù)庫:
- 創(chuàng)建SQLite數(shù)據(jù)庫;
- 向表中插入記錄;
- 其他數(shù)據(jù)庫操作。
面試題:
- 1.面試題一:如何創(chuàng)建SQLite數(shù)據(jù)庫?
- 2.面試題二:如何向SQLite表中插入數(shù)據(jù)?
- 3.面試題三:如何查詢SQLite表中的數(shù)據(jù)?
1.創(chuàng)建SQLite數(shù)據(jù)庫
# 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ù)庫成功')
- 我們通過上述操作已經(jīng)成功創(chuàng)建了sql數(shù)據(jù)庫,并在里面創(chuàng)建了一張表。
- 為了查看我們創(chuàng)建的表,我們可以用到SqliteStudio,它是一款 Sqlite數(shù)據(jù)庫可視化工具,是使用Sqlite數(shù)據(jù)庫開發(fā)應(yīng)用的必備軟件,軟件無需安裝,下載后解壓即可使用,很小巧但很了用,綠色中文版本。比起其它SQLite管理工具,我喜歡用這個。很方便易用,不用安裝的單個可執(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)是個Cursor對象(可迭代對象) 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模塊使這個list類型的result轉(zhuǎn)為字符串類型 # 網(wǎng)絡(luò)傳輸需要使用字符串類型 import json resultStr = json.dumps(result, ensure_ascii=False) print(resultStr)
總結(jié)
使用sqlite3模塊中的API可以操作SQLite數(shù)據(jù)庫,該模塊是Python內(nèi)置的模塊,不需要單獨安裝。
到此這篇關(guān)于Python練習(xí)之操作SQLite數(shù)據(jù)庫的文章就介紹到這了,更多相關(guān)Python操作SQLite 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中如何使用sqlite3操作SQLite數(shù)據(jù)庫詳解
- 使用Python連接SQLite數(shù)據(jù)庫的操作步驟
- 通過python封裝SQLite3的示例代碼
- Python數(shù)據(jù)庫編程之SQLite和MySQL的實踐指南
- Python的sqlite3模塊中常用函數(shù)
- Python中SQLite數(shù)據(jù)庫的使用
- Python數(shù)據(jù)庫sqlite3圖文實例詳解
- Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟
- python處理SQLite數(shù)據(jù)庫的方法
- SQLite5-使用Python來讀寫數(shù)據(jù)庫
- Pandas使用SQLite3實戰(zhàn)
相關(guān)文章
python中CURL 和python requests的相互轉(zhuǎn)換實現(xiàn)
本文主要介紹了python中CURL 和python requests的相互轉(zhuǎn)換實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析
這篇文章主要介紹了Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07Python?matplotlib?plotly繪制圖表詳解
plotly本身是個生態(tài)非常復(fù)雜的繪圖工具,它對很多編程語言提供接口。交互式和美觀易用應(yīng)該是?Plotly?最大的優(yōu)勢,而?Matplotlib?的特點則是可定制化程度高,但語法也相對難學(xué),各有優(yōu)缺點。本文將通過示例詳細講解二者是如何繪制圖表的,需要的可以參考一下2022-03-03