1. SQLite操作
1.1了解數據庫
1.2 操作數據庫步驟
# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 03_SQLite3添加數據.py
@IDE : PyCharm
@Author : 劉慶東
@Date : 2025/9/15 14:05
"""
# 1. 導入模塊
import sqlite3
"""連接到SQLite數據庫 數據庫文件 mrsoft.db 如果文件不存在自動幫我們創建ctrl+Alt +T 出現塊提示
"""# 2. 獲取數據庫連接
try:conn=sqlite3.connect('mrsoft.db')# 3. 創建游標對象 Cursorcursor = conn.cursor()# 4. 執行sql操作# cursor.execute('insert into user (id,name) values ("1","guangtouqiang")')# cursor.execute('insert into user (id,name) values ("2","李老板")')# cursor.execute('insert into user (id,name) values ("3","熊大")')"""上面三行代碼 能實現效果 但是不好 "sql注入攻擊"select * from admin where name='liushao 'or 1=1 or''下面是安全的寫法!!!!"""#使用sql預編譯模式 防止sql注入攻擊# sql="insert into user (id,name) values (?,?)"# cursor.execute(sql,(4,'張無忌'))# 一次性放入多條數據 [使用數據容器中的字典 ]sql = "insert into user (id,name) values (?,?)"data =[(5,"成昆"),(6,"周芷若"),(7,"宋青書")]cursor.executemany(sql,data)print("一次存儲多條數據")# 5. 關閉游標cursor.close()# 6. 提交事務conn.commit()
except Exception as e:print(e)
finally:# 7. 關閉數據庫連接conn.close()
1.2查詢數據三種方式fetchone():獲取查詢結果集中的下一條記錄。fetchmany(size):獲取指定數量的記錄。fetchall():獲取結構集的所有記錄。
# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 04_SQLite3查詢.py
@IDE : PyCharm
@Author : 劉慶東
@Date : 2025/9/15 14:30
"""
# 1. 導入模塊
import sqlite3
"""連接到SQLite數據庫 數據庫文件 mrsoft.db 如果文件不存在自動幫我們創建ctrl+Alt +T 出現塊提示
"""# 2. 獲取數據庫連接
try:conn=sqlite3.connect('mrsoft.db')# 3. 創建游標對象 Cursorcursor = conn.cursor()# 4. 操作sql cursor.execute("select * from user")# 查詢一條數據result1=cursor.fetchone()print(result1)
"""
控制臺輸出結果是:
(1, 'guangtouqiang')
"""# 查詢前兩條數據result2 = cursor.fetchmany(2)print(result2)"""
控制臺輸出結果是:
[(1, 'guangtouqiang'), (2, '李老板')]
"""# 提取全部的數據result3=cursor.fetchall()print(result3)
"""
控制臺輸出結果是:
[(1, 'guangtouqiang'), (2, '李老板'), (3, '熊大'), (4, '張無忌'), (5, '成昆'), (6, '周芷若'), (7, '宋青書')]
"""# 查詢的時候 不對數據庫的表做改變 可以不使用事務 !!!# 6. 關閉游標cursor.close()
except Exception as e:print(e)
finally:# 7. 關閉數據庫連接conn.close()
更新操作
# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 05_SQLite更新.py
@IDE : PyCharm
@Author : 劉慶東
@Date : 2025/9/15 14:52
"""
# 1.引入模塊
import sqlite3try:# 2. 創建數據庫連接對象 Connectionconnection = sqlite3.connect("mrsoft.db")# 3. 獲取游標cursor = connection.cursor()# 4. 執行sql語句操作cursor.execute('update user set name=? where id=?',('光頭強',1))# 立刻做查詢cursor.execute("select * from user")#讀取數據result=cursor.fetchall()print(result)# 5.關閉游標cursor.close()# 6. 提交事務connection.commit()
except Exception as e:print(e)# 打印的是錯誤的堆棧信息
finally:# 7.關閉數據庫連接connection.close()
刪除操作
# -*- coding: utf-8 -*-
"""
@Project : 01-python-learn
@File : 06_SQLite刪除.py
@IDE : PyCharm
@Author : 劉慶東
@Date : 2025/9/15 14:56
"""# 1.引入模塊
import sqlite3try:# 2. 創建數據庫連接對象 Connectionconnection = sqlite3.connect("mrsoft.db")# 3. 獲取游標cursor = connection.cursor()# 4. 執行sql語句操作cursor.execute('delete from user where id=?',(5,))#注意了一個參數也要寫 ,#剛才沒有寫查詢的sql語句cursor.execute(('select * from user'))result=cursor.fetchall()# 立刻做查詢print(result)# 5.關閉游標cursor.close()# 6. 提交事務connection.commit()
except Exception as e:print(e) # 打印的是錯誤的堆棧信息
finally:# 7.關閉數據庫連接connection.close()