Python使用sqlite3模塊內(nèi)置數(shù)據(jù)庫
1、python內(nèi)置的sqlite3模塊,創(chuàng)建數(shù)據(jù)庫中的表,并向表中插入數(shù)據(jù),從表中取出所有行,以及輸出行的數(shù)量。
#!/usr/bin/env python3 #創(chuàng)建SQLite3內(nèi)存數(shù)據(jù)庫,并創(chuàng)建帶有四個屬性的sales表 #sqlite3模塊,提供了一個輕量級的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程 import sqlite3 #使用‘:memory:'在內(nèi)存中創(chuàng)建了一個數(shù)據(jù)庫,創(chuàng)建了連接對象con來代表數(shù)據(jù)庫 con = sqlite3.connect(':memory:') #創(chuàng)建表名為sales的表,將這個字符串賦值給query query = """CREATE TABLE sales (customer VARCHAR(20), product VARCHAR(40), amount FLOAT, date DATE);""" #使用連接對象的execute()方法執(zhí)行query中的SQL命令 con.execute(query) #使用連接對象的commit()方法將修改提交(保存)到數(shù)據(jù)庫 con.commit() #向表中插入幾行數(shù)據(jù) data = [('Richard Lucas','Notepad',2.50,'2019-01-02'), ('Jenny Kim','Binder',4.15,'2019-01-05'), ('Svetlana Crow','Printer',155.75,'2019-02-03'), ('Stephen Randolph','Computer',679.40,'2019-02-20')] #將插入語句賦給變量statement,?是占位符 statement = "INSERT INTO sales VALUES(?,?,?,?)" #因?yàn)橛兴膫€占位符,這里就需要提供一個包含4個值的元組,executemany()方法為data中的每個數(shù)據(jù)元組執(zhí)行 #statement中的SQL命令,這里執(zhí)行了四次insert命令 con.executemany(statement,data) #將修改保存到數(shù)據(jù)庫 con.commit() #查詢sales表,并將命令結(jié)果賦值給一個光標(biāo)對象cursor,光標(biāo)對象有execute、executemany、fetchone、 #fetchmany和fetchall方法 cursor = con.execute("SELECT * FROM sales") #返回結(jié)果集中的所有行 rows = cursor.fetchall() print(rows) print('………………') #查詢結(jié)果中行的數(shù)量 row_counter = 0 for row in rows: print(row) row_counter += 1 print('………………') print('Number of rows: %d' % (row_counter))
Spyder右下角打印出來的結(jié)果:
[('Richard Lucas', 'Notepad', 2.5, '2019-01-02'), ('Jenny Kim', 'Binder', 4.15, '2019-01-05'), ('Svetlana Crow', 'Printer', 155.75, '2019-02-03'), ('Stephen Randolph', 'Computer', 679.4, '2019-02-20')] ……………… ('Richard Lucas', 'Notepad', 2.5, '2019-01-02') ('Jenny Kim', 'Binder', 4.15, '2019-01-05') ('Svetlana Crow', 'Printer', 155.75, '2019-02-03') ('Stephen Randolph', 'Computer', 679.4, '2019-02-20') ……………… Number of rows: 4
2、python內(nèi)置的sqlite3模塊,向表中插入新紀(jì)錄
名稱為“CSV測試數(shù)據(jù).csv”的數(shù)據(jù)源:
將本地“CSV測試數(shù)據(jù).csv”的數(shù)據(jù)導(dǎo)入到本地數(shù)據(jù)庫football_game.db中:
#!/usr/bin/env python3 #創(chuàng)建SQLite3內(nèi)存數(shù)據(jù)庫,并創(chuàng)建帶有四個屬性的sales表 #sqlite3模塊,提供了一個輕量級的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程 import sqlite3 import csv input_file = "F://python入門//數(shù)據(jù)1//CSV測試數(shù)據(jù).csv" #為一個簡單的本地數(shù)據(jù)庫football_game.db創(chuàng)建連接,football_game.db為數(shù)據(jù)庫名稱 con = sqlite3.connect('football_game.db') #創(chuàng)建了一個光標(biāo) c = con.cursor() #如果表名存在,則刪除它 drop_table = """DROP TABLE IF EXISTS football_game;""" c.execute(drop_table) con.commit() #創(chuàng)建表名為football_game的表,將這個字符串賦值給create_table create_table = """CREATE TABLE IF NOT EXISTS football_game (name VARCHAR(20), sex VARCHAR(10), age INT, score INT, device_number VARCHAR(20), cost VARCHAR(20));""" #使用連接對象的execute()方法執(zhí)行create_table中的SQL命令 c.execute(create_table) #使用連接對象的commit()方法將修改提交(保存)到數(shù)據(jù)庫 con.commit() #從CSV格式的輸入文件中讀取要加載到數(shù)據(jù)庫中的數(shù)據(jù),創(chuàng)建file_reader對象,用于存儲CSV中的數(shù)據(jù)集 file_reader = csv.reader(open(input_file,'r'),delimiter=',') #從輸入文件中讀入第一行 header = next(file_reader,None) #將輸入的所有數(shù)據(jù)進(jìn)行循環(huán),先是每行循環(huán),再是每列循環(huán) for row in file_reader: data = [] for column_index in range(len(header)): data.append(row[column_index]) print(data) c.execute("INSERT INTO football_game VALUES(?,?,?,?,?,?)",data) #將修改保存到數(shù)據(jù)庫 con.commit() print('………………') #執(zhí)行選擇所有數(shù)據(jù)的SQL output = c.execute("SELECT * FROM football_game") #返回結(jié)果集中的所有行,返回的是一個大的列表 rows = output.fetchall() print(rows) print('………………') for row in rows: output = [] for column_index in range(len(row)): output.append(str(row[column_index])) print(output)
Spyder右下角打印出來的結(jié)果:
['李剛', '男', '32', '567', '18512349553', '$500.00 '] ['王紅', '女', '54', '423', '18256785181', '$750.00 '] ['孫曉', '女', '25', '457', '13698762112', '$250.00 '] ['郭亮', '男', '65', '350', '18654320816', '$125.00 '] ['高英', '女', '15', '390', '18511113141', '$815.00 '] ……………… [('李剛', '男', 32, 567, '18512349553', '$500.00 '), ('王紅', '女', 54, 423, '18256785181', '$750.00 '), ('孫曉', '女', 25, 457, '13698762112', '$250.00 '), ('郭亮', '男', 65, 350, '18654320816', '$125.00 '), ('高英', '女', 15, 390, '18511113141', '$815.00 ')] ……………… ['李剛', '男', '32', '567', '18512349553', '$500.00 '] ['王紅', '女', '54', '423', '18256785181', '$750.00 '] ['孫曉', '女', '25', '457', '13698762112', '$250.00 '] ['郭亮', '男', '65', '350', '18654320816', '$125.00 '] ['高英', '女', '15', '390', '18511113141', '$815.00 ']
3、python內(nèi)置的sqlite3模塊,更新數(shù)據(jù)表中的記錄
名稱為“CSV測試數(shù)據(jù).csv”的數(shù)據(jù)源:
更新表中的記錄:
#!/usr/bin/env python3 #創(chuàng)建SQLite3內(nèi)存數(shù)據(jù)庫,并創(chuàng)建帶有四個屬性的sales表 #sqlite3模塊,提供了一個輕量級的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程 import sqlite3 import csv input_file = "F://python入門//數(shù)據(jù)1//CSV測試數(shù)據(jù).csv" #使用‘:memory:'在內(nèi)存中創(chuàng)建了一個數(shù)據(jù)庫,創(chuàng)建了連接對象con來代表數(shù)據(jù)庫 con = sqlite3.connect(':memory:') #創(chuàng)建表名為sales的表,將這個字符串賦值給query query = """CREATE TABLE IF NOT EXISTS sales (customer VARCHAR(20), product VARCHAR(40), amount FLOAT, date DATE);""" #使用連接對象的execute()方法執(zhí)行query中的SQL命令 con.execute(query) #使用連接對象的commit()方法將修改提交(保存)到數(shù)據(jù)庫 con.commit() #向表中插入幾行數(shù)據(jù) data = [('Richard Lucas','Notepad',2.50,'2019-01-02'), ('Jenny Kim','Binder',4.15,'2019-01-05'), ('Svetlana Crow','Printer',155.75,'2019-02-03'), ('Stephen Randolph','Computer',679.40,'2019-02-20')] #for tuple in data: # print(tuple) #將插入語句賦給變量statement,?是占位符 statement = "INSERT INTO sales VALUES(?,?,?,?)" #因?yàn)橛兴膫€占位符,這里就需要提供一個包含4個值的元組,executemany()方法為data中的每個數(shù)據(jù)元組執(zhí)行 #statement中的SQL命令,這里執(zhí)行了四次insert命令 con.executemany(statement,data) #將修改保存到數(shù)據(jù)庫 con.commit() #讀取CSV文件并更新特定的行 file_reader = csv.reader(open(input_file,'r'),delimiter=',') #從輸入文件中讀入第一行 header = next(file_reader,None) #將輸入的所有數(shù)據(jù)進(jìn)行循環(huán),先是每行循環(huán),再是每列循環(huán) for row in file_reader: data = [] for column_index in range(len(header)): data.append(row[column_index]) con.execute("UPDATE sales SET amount=?,date=? where customer=?;",data) #將修改保存到數(shù)據(jù)庫 con.commit() #查詢sales表,并將命令結(jié)果賦值給一個光標(biāo)對象cursor,光標(biāo)對象有execute、executemany、fetchone、 #fetchmany和fetchall方法 cursor = con.execute("SELECT * FROM sales") #返回結(jié)果集中的所有行 rows = cursor.fetchall() print(rows) print('………………') for row in rows: output = [] for column_index in range(len(row)): output.append(str(row[column_index])) print(output)
Spyder右下角打印出來的結(jié)果:
[('Richard Lucas', 'Notepad', 4.25, '2019-11-05'), ('Jenny Kim', 'Binder', 6.75, '2019-12-05'), ('Svetlana Crow', 'Printer', 155.75, '2019-02-03'), ('Stephen Randolph', 'Computer', 679.4, '2019-02-20')] ……………… ['Richard Lucas', 'Notepad', '4.25', '2019-11-05'] ['Jenny Kim', 'Binder', '6.75', '2019-12-05'] ['Svetlana Crow', 'Printer', '155.75', '2019-02-03'] ['Stephen Randolph', 'Computer', '679.4', '2019-02-20']
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python復(fù)制Word內(nèi)容并使用格式設(shè)字體與大小實(shí)例代碼
這篇文章主要介紹了Python復(fù)制Word內(nèi)容并使用格式設(shè)字體與大小實(shí)例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01python3+dlib實(shí)現(xiàn)人臉識別和情緒分析
本文通過具體代碼不步驟給大家詳細(xì)講述了python3+dlib實(shí)現(xiàn)人臉識別以及情緒分析的方法,有需要的朋友參考下。2018-04-04Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象
這篇文章主要為大家介紹了Python面向?qū)ο缶幊剃P(guān)鍵深度探索類與對象示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05