Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫(kù)的方法
本文實(shí)例講述了Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫(kù)的方法,對(duì)于Python的學(xué)習(xí)有不錯(cuò)的參考借鑒價(jià)值。分享給大家供大家參考之用。具體方法如下:
實(shí)例代碼如下:
import sqlite3 db = r"D:\pyWork\test.db" #pyWork目錄下test.db數(shù)據(jù)庫(kù)文件 drp_tb_sql = "drop table if exists staff" crt_tb_sql = """ create table if not exists staff( id integer primary key autoincrement unique not null, name varchar(100), city varchar(100) ); """ #連接數(shù)據(jù)庫(kù) con = sqlite3.connect(db) cur = con.cursor() #創(chuàng)建表staff cur.execute(drp_tb_sql) cur.execute(crt_tb_sql) #插入記錄 insert_sql = "insert into staff (name,city) values (?,?)" #?為占位符 cur.execute(insert_sql,('Tom','New York')) cur.execute(insert_sql,('Frank','Los Angeles')) cur.execute(insert_sql,('Kate','Chicago')) cur.execute(insert_sql,('Thomas','Houston')) cur.execute(insert_sql,('Sam','Philadelphia')) con.commit() #查詢記錄 select_sql = "select * from staff" cur.execute(select_sql) #返回一個(gè)list,list中的對(duì)象類型為tuple(元組) date_set = cur.fetchall() for row in date_set: print(row) cur.close() con.close()
希望本文實(shí)例對(duì)大家的Python學(xué)習(xí)有所幫助。
- Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實(shí)例
- Python標(biāo)準(zhǔn)庫(kù)之sqlite3使用實(shí)例
- Python操作SQLite簡(jiǎn)明教程
- python操作數(shù)據(jù)庫(kù)之sqlite3打開(kāi)數(shù)據(jù)庫(kù)、刪除、修改示例
- python從sqlite讀取并顯示數(shù)據(jù)的方法
- Python SQLite3數(shù)據(jù)庫(kù)操作類分享
- Python Sqlite3以字典形式返回查詢結(jié)果的實(shí)現(xiàn)方法
- Python使用flask框架操作sqlite3的兩種方式
- Python實(shí)現(xiàn)讀取TXT文件數(shù)據(jù)并存進(jìn)內(nèi)置數(shù)據(jù)庫(kù)SQLite3的方法
- Python連接SQLite數(shù)據(jù)庫(kù)并進(jìn)行增冊(cè)改查操作方法詳解
相關(guān)文章
Python?ConfigParser庫(kù)輕松讀寫INI文件實(shí)例探究
這篇文章主要為大家介紹了Python?ConfigParser庫(kù)輕松讀寫INI文件實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python實(shí)現(xiàn)剪刀石頭布小游戲(與電腦對(duì)戰(zhàn))
這篇文章給大家分享Python基礎(chǔ)實(shí)現(xiàn)與電腦對(duì)戰(zhàn)的剪刀石頭布小游戲,練習(xí)if while輸入和輸出,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-12-12Python繪圖示例程序中的幾個(gè)語(yǔ)法糖果你知道嗎
這篇文章主要為大家詳細(xì)介紹了Python繪圖示例程序中的幾個(gè)語(yǔ)法糖果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02Python+uiautomator2實(shí)現(xiàn)手機(jī)鎖屏解鎖功能
python-uiautomator2封裝了谷歌自帶的uiautomator2測(cè)試框架,提供便利的python接口,這篇文章給大家介紹使用Python+uiautomator2實(shí)現(xiàn)手機(jī)鎖屏解鎖(期望輸入的鎖屏密碼,基于滑動(dòng)解鎖),感興趣的朋友一起看看吧2021-04-04運(yùn)行python提示no module named sklearn的解決方法
這篇文章主要介紹了運(yùn)行python提示no module named sklearn的解決方法,需要的朋友可以參考下2020-11-11