Python3實現(xiàn)連接SQLite數(shù)據(jù)庫的方法
更新時間:2014年08月23日 14:53:14 投稿:shichen2014
這篇文章主要介紹了Python3實現(xiàn)連接SQLite數(shù)據(jù)庫的方法,在Python數(shù)據(jù)庫編程中有著廣泛的應用,需要的朋友可以參考下
本文實例講述了Python3實現(xiàn)連接SQLite數(shù)據(jù)庫的方法,對于Python的學習有不錯的參考借鑒價值。分享給大家供大家參考之用。具體方法如下:
實例代碼如下:
import sqlite3 db = r"D:\pyWork\test.db" #pyWork目錄下test.db數(shù)據(jù)庫文件 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ù)庫 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) #返回一個list,list中的對象類型為tuple(元組) date_set = cur.fetchall() for row in date_set: print(row) cur.close() con.close()
希望本文實例對大家的Python學習有所幫助。
您可能感興趣的文章:
- Python操作sqlite3快速、安全插入數(shù)據(jù)(防注入)的實例
- Python標準庫之sqlite3使用實例
- Python操作SQLite簡明教程
- python操作數(shù)據(jù)庫之sqlite3打開數(shù)據(jù)庫、刪除、修改示例
- python從sqlite讀取并顯示數(shù)據(jù)的方法
- Python SQLite3數(shù)據(jù)庫操作類分享
- Python Sqlite3以字典形式返回查詢結(jié)果的實現(xiàn)方法
- Python使用flask框架操作sqlite3的兩種方式
- Python實現(xiàn)讀取TXT文件數(shù)據(jù)并存進內(nèi)置數(shù)據(jù)庫SQLite3的方法
- Python連接SQLite數(shù)據(jù)庫并進行增冊改查操作方法詳解
相關(guān)文章
Python?ConfigParser庫輕松讀寫INI文件實例探究
這篇文章主要為大家介紹了Python?ConfigParser庫輕松讀寫INI文件實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01Python實現(xiàn)剪刀石頭布小游戲(與電腦對戰(zhàn))
這篇文章給大家分享Python基礎(chǔ)實現(xiàn)與電腦對戰(zhàn)的剪刀石頭布小游戲,練習if while輸入和輸出,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-12-12Python+uiautomator2實現(xiàn)手機鎖屏解鎖功能
python-uiautomator2封裝了谷歌自帶的uiautomator2測試框架,提供便利的python接口,這篇文章給大家介紹使用Python+uiautomator2實現(xiàn)手機鎖屏解鎖(期望輸入的鎖屏密碼,基于滑動解鎖),感興趣的朋友一起看看吧2021-04-04運行python提示no module named sklearn的解決方法
這篇文章主要介紹了運行python提示no module named sklearn的解決方法,需要的朋友可以參考下2020-11-11