python 操作sqlite用法

2021-09-30 10:38:57 字數 1471 閱讀 7710

sqlite資料庫是非常小巧,非常適用於嵌入式軟體開發,且占用資源非常低。

開啟資料庫時返回的物件是乙個資料庫連線物件,它可以有以下操作:

commit()--事務提交    

rollback()--事務回滾   

close()--關閉乙個資料庫連線   

cursor()--建立乙個游標

游標物件有以下的操作:

execute()--執行sql語句   

executemany--執行多條sql語句   

close()--關閉游標   

fetchone()--從結果中取一條記錄,並將游標指向下一條記錄   

fetchmany()--從結果中取多條記錄   

fetchall()--從結果中取出所有記錄   

scroll()--游標滾動  

in [1]: import sqlite3

in [2]: cx=sqlite3.connect('test.db')#如果不存在就指定乙個

in [4]: cu=cx.cursor()#游標物件

in [5]: cu.execute("create table catalog (id integer primary key,pid integer,name varchar(10) unique,nickname text null)")#建表

out[5]: in [6]:

in [7]: for t in[(0,10,'abc','yu'),(1,20,'cba','xu')]:

...: cx.execute("insert into catalog values (?,?,?,?)", t)

...:

out[7]: out[7]: in [8]: cx.commit()#修改以後再提交

in [10]: cu.execute('select * from catalog')#查詢

out[11]: in [12]: cu.fetchall()

out[12]: [(0, 10, u'abc', u'yu'), (1, 20, u'cba', u'xu')]

in [13]: cu.execute("update catalog set name='boy' where id = 0")

out[13]: in [14]: cx.commit()

in [15]: cu.execute("delete from catalog where id = 1")

out[15]: in [16]: cx.commit() #修改以後再提交

in [17]: cu.execute('select * from catalog')

out[17]: in [18]: cu.fetchall()

out[18]: [(0, 10, u'boy', u'yu')]

python 連線sqlite及操作

import sqlite3 查詢def load table 連線資料庫 con sqlite3.connect e datebase sqlitestudio park.db 獲得游標 cur con.cursor 查詢整個表 cur.execute select from table list...

Python 操作sqlite資料庫

sqlite是一種輕量級的資料庫,它最大的特點就是無需安裝。資料庫本身以乙個單獨的檔案的形式存放。sqlite只有5種資料型別 null 值是乙個 null 值。integer 值是乙個帶符號的整數,根據值的大小儲存在 1 2 3 4 6 或 8 位元組中。real 值是乙個浮點值,儲存為 8 位元...

python操作sqlite資料庫

sqlite資料庫是一款輕量級的資料庫,無伺服器 零配置 事務性的sql資料庫引擎。sqlite是世界上最廣泛部署的sql資料庫引擎,而且sqlite的源 不受版權限制,是小型專案和簡單web應用的理想選擇。sqlite資料庫是乙個單一的,不依賴於其他模組與元件的資料庫檔案,它允許我們直接訪問儲存檔...