學習筆記 python使用SQLite

2021-10-24 23:01:17 字數 4245 閱讀 6258

python運算元據庫系列

python使用sqlite

python操作redis

python操作mysql

5.修改資料

6.刪除資料

三、全部**

四、總結

sqlite不是乙個客服端/伺服器結構的資料庫引擎,而是一種嵌入式資料庫,它的資料庫就是乙個檔案。sqlite將整個資料庫,包括定義、表、索引以及資料本身作為乙個單獨的、可跨平台的檔案儲存在主機中。由於sqlite本身是c寫的,而且體積很小,所以被經常整合到各種應用程式中。python就內建了sqlite3,所以在python中使用sqlite,不需要安裝任何模組,直接使用。

**如下(示例):

import sqlite3
使用sqliter3.connect()方法連線sqlite資料庫檔案database.db,由於檔案不存在,所以會在同級目錄建立檔案,該檔案包含student相關資訊。

**如下(示例):

# 連線到sqlite資料庫並建立database.db檔案

connection = sqlite3.connect(

'database.db'

)# 建立cursor游標

cursor = connection.cursor(

)# 執行sql語句,建立student表

cursor.execute(

'create table student(id int(10) primary key, name varchar(20))'

)# 關閉游標

cursor.close(

)# 關閉connection

connection.close(

)

再次執行次**,會提示錯誤資訊,sqlite3.operationalerror:table student alread exists說明表student已經存在.

向student表中插入三條資料,此外由於新增資料,需要使用commit()方法提交事務。因為對於增加、修改和刪除操作,使用commit()方法提交事務後,如果相應的操作失敗,可以使用rollback()方法回滾到操作之前的狀態。

insert into 表名(欄位名1,欄位名2,…,欄位名n) values (字段值1,字段值1,字段值2,…,字段值n)

**如下(示例):

# 執行一條sql語句,插入一條記錄

curosr.execute(

'insert into student(id ,name) values ("1", "xiaoming")'

)curosr.execute(

'insert into student(id ,name) values ("2", "zhangsan")'

)curosr.execute(

'insert into student(id ,name) values ("2", "lisi")'

)

# 提交事務

connection.commit(

)

再次執行**,如果提示下面資訊證明已經插入成功,表中已存在資料,報如下錯誤:

sqlite3.integrityerror: unique constraint failed: student.id

select 欄位名1,欄位名2,欄位名3,…from 表名 where 查詢條件

fetchmany(size):獲取指定數量的記錄

fetchall():獲取所有記錄

**如下(示例):

# 執行查詢語句

cursor.execute(

'select * from student'

)# 獲取查詢結果

result1 = cursor.fetchone(

)# print(result1)

result2 = cursor.fetchmany(2)

# print(result2)

result3 = cursor.fetchall(

)# print(result3)

**如下(示例):

# 查詢id>1的符合所有資訊

cursor.execute(

'select * from student where id > 1'

)

# 查詢id>?的符合所有資訊,用元組(1, )代替問號

cursor.execute(

'select * from student where id > ?',(

1,))

語法:update 表名 set 欄位名 = 字段值 where 查詢條件

# 更新資料

cursor.execute(

'update student set name = ? where id = ?',(

'xiaohuangfeng',1

))

**如下(示例):

delete from 表名 where 查詢條件

# 刪除資料

cursor.execute(

'delete from student where id = ?',(

3,))

注釋掉的**自己嘗試更改或取消

import sqlite3

# 連線到sqlite資料庫並建立database.db檔案

connection = sqlite3.connect(

'database.db'

)# 建立cursor游標

cursor = connection.cursor(

)# 執行sql語句,建立student表

cursor.execute(

'create table student(id int(10) primary key, name varchar(20))'

)# 執行一條sql語句,插入一條記錄

cursor.execute(

'insert into student(id, name) values("1", "xiaoming")'

)cursor.execute(

'insert into student(id, name) values("2", "zhangsan")'

)cursor.execute(

'insert into student(id, name) values("3", "lisi")'

)# 修改資料

cursor.execute(

'update student set name = ? where id = ?',(

'xiaohuangfeng',1

))# 刪除資料

cursor.execute(

'delete from student where id = ?',(

3,))

# 執行查詢語句

cursor.execute(

'select * from student'

)# cursor.execute('select * from student where id > 1')

# cursor.execute('select * from student where id > ?', (1, ))

# 獲取查詢結果

# result1 = cursor.fetchone()

# print(result1)

# result2 = cursor.fetchmany(2)

# print(result2)

result3 = cursor.fetchall(

)print

(result3)

# 關閉游標

cursor.close(

)# 提交事務

connection.commit(

)# 關閉connection

connection.close(

)

SQL 學習筆記 ( )

sql 語法特點 1 每個語句以 結束,中間用 包含,間隔。2 對變數和常量等需要用 包含。3 關鍵字大寫,變數名首大寫字母用表名表徵,然後用小寫表徵屬性 基本語句 desc 表名 檢視表詳細屬性 建表 create table 表名 屬性名 型別 約束 主鍵 primary key 如果是域為主鍵...

sql學習筆記

declare rc int declare stattime datetime declare endtime datetime select stattime 2008 06 22 00 00 00.000 select endtime 2008 06 22 01 00 00.000 while...

SQL學習筆記

一 資料庫的建立與刪除 create database demo drop database demo 二 表的定義與更改 1 表的定義 建立乙個學生情況表student create table student st class char 8 st no char 10 not null,st n...