Python簡單操作sqlite3的方法示例
更新時間:2017年03月22日 10:43:41 作者:聰明的狐貍
這篇文章主要介紹了Python簡單操作sqlite3的方法,結合實例形式分析了Python針對sqlite3數(shù)據(jù)庫的讀取、創(chuàng)建、增刪改查等基本操作技巧,需要的朋友可以參考下
本文實例講述了Python簡單操作sqlite3的方法。分享給大家供大家參考,具體如下:
import sqlite3
def Test1():
#con =sqlite3.connect("D:\\test.db")
con =sqlite3.connect(":memory:") #store in memory
cur =con.cursor()
try:
cur.execute('create table score(id integer primary key,name varchar(10),scores integer)')
cur.execute("insert into score values(0,'Rose',87)")
cur.execute("insert into score values(1,'Alice',78)")
cur.execute("insert into score values(2,'Helon',100)")
cur.execute("insert into score values(3,'Tom',98)")
cur.execute("insert into score values(4,'jack',198)")
cur.execute("insert into score values(5,'Tony',198)")
cur.execute("insert into score values(6,'David',99)")
cur.execute("update score set scores =? where id=?",(45,3))
cur.execute("update score set name=? where id=?",("John",0))
cur.execute("delete from score where id =1")
except Exception,e:
print "There are some except",e
con.commit()
print "Insert Complete"
print "-----------------------------------------"
print "Last row id is ",cur.lastrowid
cur.execute('select * from score')
print cur.fetchall()
print "----------------------------------------"
cur.execute("select count(*) from score")
print "Current Rows is :",cur.fetchall()[0]
cur.close()
con.close()
if __name__ =='__main__':
Test1()
print "hello world"
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python常見數(shù)據(jù)庫操作技巧匯總》、《Python編碼操作技巧總結》、《Python圖片操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python Socket編程技巧總結》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
解決python3 json數(shù)據(jù)包含中文的讀寫問題
這篇文章主要介紹了解決python3 json數(shù)據(jù)包含中文的讀寫問題,需要的朋友可以參考下2021-05-05
Pytest單元測試框架生成HTML測試報告及優(yōu)化的步驟
本文主要介紹了Pytest單元測試框架生成HTML測試報告及優(yōu)化的步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

