Python練習(xí)之操作SQLite數(shù)據(jù)庫
前言
文章包括下幾點(diǎn):
考點(diǎn)--操作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)置的模塊,不需要單獨(dú)安裝。
到此這篇關(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的實(shí)踐指南
- Python的sqlite3模塊中常用函數(shù)
- Python中SQLite數(shù)據(jù)庫的使用
- Python數(shù)據(jù)庫sqlite3圖文實(shí)例詳解
- Python使用sqlite3第三方庫讀寫SQLite數(shù)據(jù)庫的方法步驟
- python處理SQLite數(shù)據(jù)庫的方法
- SQLite5-使用Python來讀寫數(shù)據(jù)庫
- Pandas使用SQLite3實(shí)戰(zhàn)
相關(guān)文章
python實(shí)現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法
這篇文章主要介紹了python實(shí)現(xiàn)超簡單端口轉(zhuǎn)發(fā)的方法,實(shí)例分析了Python同構(gòu)socket實(shí)現(xiàn)端口轉(zhuǎn)發(fā)的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Python實(shí)現(xiàn)的幾個常用排序算法實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)的幾個常用排序算法實(shí)例例如直接插入排序、直接選擇排序、冒泡排序、快速排序等,需要的朋友可以參考下2014-06-06
淺析python 中__name__ = ''__main__'' 的作用
這篇文章主要介紹了python 中__name__ = '__main__' 的作用,對于初學(xué)者來說很有幫助,需要的朋友可以參考下2014-07-07
Pycharm?debug程序,跳轉(zhuǎn)至指定循環(huán)條件/循環(huán)次數(shù)問題
這篇文章主要介紹了Pycharm?debug程序,跳轉(zhuǎn)至指定循環(huán)條件/循環(huán)次數(shù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
python3中calendar返回某一時間點(diǎn)實(shí)例講解
在本篇內(nèi)容里小編給大家整理了關(guān)于python3中calendar返回某一時間點(diǎn)實(shí)例講解內(nèi)容,有興趣的朋友們可以參考學(xué)習(xí)下。2020-11-11

