国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python使用sqlite3模塊內(nèi)置數(shù)據(jù)庫

瀏覽:57日期:2022-07-26 10:26:11

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)建帶有四個(gè)屬性的sales表#sqlite3模塊,提供了一個(gè)輕量級(jí)的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程import sqlite3#使用‘:memory:’在內(nèi)存中創(chuàng)建了一個(gè)數(shù)據(jù)庫,創(chuàng)建了連接對(duì)象con來代表數(shù)據(jù)庫con = sqlite3.connect(’:memory:’)#創(chuàng)建表名為sales的表,將這個(gè)字符串賦值給queryquery = '''CREATE TABLE sales (customer VARCHAR(20), product VARCHAR(40), amount FLOAT, date DATE);'''#使用連接對(duì)象的execute()方法執(zhí)行query中的SQL命令con.execute(query)#使用連接對(duì)象的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)橛兴膫€(gè)占位符,這里就需要提供一個(gè)包含4個(gè)值的元組,executemany()方法為data中的每個(gè)數(shù)據(jù)元組執(zhí)行#statement中的SQL命令,這里執(zhí)行了四次insert命令con.executemany(statement,data)#將修改保存到數(shù)據(jù)庫con.commit()#查詢sales表,并將命令結(jié)果賦值給一個(gè)光標(biāo)對(duì)象cursor,光標(biāo)對(duì)象有execute、executemany、fetchone、#fetchmany和fetchall方法cursor = con.execute('SELECT * FROM sales')#返回結(jié)果集中的所有行rows = cursor.fetchall()print(rows)print(’………………’)#查詢結(jié)果中行的數(shù)量row_counter = 0for row in rows: print(row) row_counter += 1print(’………………’)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測(cè)試數(shù)據(jù).csv”的數(shù)據(jù)源:

Python使用sqlite3模塊內(nèi)置數(shù)據(jù)庫

將本地“CSV測(cè)試數(shù)據(jù).csv”的數(shù)據(jù)導(dǎo)入到本地?cái)?shù)據(jù)庫football_game.db中:

#!/usr/bin/env python3#創(chuàng)建SQLite3內(nèi)存數(shù)據(jù)庫,并創(chuàng)建帶有四個(gè)屬性的sales表#sqlite3模塊,提供了一個(gè)輕量級(jí)的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程import sqlite3import csvinput_file = 'F://python入門//數(shù)據(jù)1//CSV測(cè)試數(shù)據(jù).csv'#為一個(gè)簡(jiǎn)單的本地?cái)?shù)據(jù)庫football_game.db創(chuàng)建連接,football_game.db為數(shù)據(jù)庫名稱con = sqlite3.connect(’football_game.db’)#創(chuàng)建了一個(gè)光標(biāo)c = con.cursor()#如果表名存在,則刪除它drop_table = '''DROP TABLE IF EXISTS football_game;'''c.execute(drop_table)con.commit()#創(chuàng)建表名為football_game的表,將這個(gè)字符串賦值給create_tablecreate_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));'''#使用連接對(duì)象的execute()方法執(zhí)行create_table中的SQL命令c.execute(create_table)#使用連接對(duì)象的commit()方法將修改提交(保存)到數(shù)據(jù)庫con.commit()#從CSV格式的輸入文件中讀取要加載到數(shù)據(jù)庫中的數(shù)據(jù),創(chuàng)建file_reader對(duì)象,用于存儲(chǔ)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ù)的SQLoutput = c.execute('SELECT * FROM football_game')#返回結(jié)果集中的所有行,返回的是一個(gè)大的列表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測(cè)試數(shù)據(jù).csv”的數(shù)據(jù)源:

Python使用sqlite3模塊內(nèi)置數(shù)據(jù)庫

更新表中的記錄:

#!/usr/bin/env python3#創(chuàng)建SQLite3內(nèi)存數(shù)據(jù)庫,并創(chuàng)建帶有四個(gè)屬性的sales表#sqlite3模塊,提供了一個(gè)輕量級(jí)的基于磁盤的數(shù)據(jù)庫,不需要獨(dú)立的服務(wù)器進(jìn)程import sqlite3import csvinput_file = 'F://python入門//數(shù)據(jù)1//CSV測(cè)試數(shù)據(jù).csv'#使用‘:memory:’在內(nèi)存中創(chuàng)建了一個(gè)數(shù)據(jù)庫,創(chuàng)建了連接對(duì)象con來代表數(shù)據(jù)庫con = sqlite3.connect(’:memory:’)#創(chuàng)建表名為sales的表,將這個(gè)字符串賦值給queryquery = '''CREATE TABLE IF NOT EXISTS sales (customer VARCHAR(20), product VARCHAR(40), amount FLOAT, date DATE);'''#使用連接對(duì)象的execute()方法執(zhí)行query中的SQL命令con.execute(query)#使用連接對(duì)象的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)橛兴膫€(gè)占位符,這里就需要提供一個(gè)包含4個(gè)值的元組,executemany()方法為data中的每個(gè)數(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é)果賦值給一個(gè)光標(biāo)對(duì)象cursor,光標(biāo)對(duì)象有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’]

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美成人综合 | 99国产国人青青视频在线观看 | 美女扒开腿让男人桶 | 国产日韩精品一区二区 | 欧美国产日韩久久久 | 亚洲男人的天堂久久无 | 国产日韩精品视频 | 久久福利资源国产精品999 | 日韩在线欧美在线 | 亚洲欧洲视频在线 | 亚洲国产高清一区二区三区 | 99精品国产在现线免费 | 亚洲欧美自拍偷拍 | 日本久久久久久久久久 | 中文字幕一区二区三区精彩视频 | 欧美三级网站在线观看 | 国产精品久久久一区二区三区 | 欧美日韩精品高清一区二区 | 全部aⅴ极品视觉盛宴精品 全部免费a级毛片 | 中国一级毛片免费观看 | 国产成人免费片在线视频观看 | 日本天堂网 | 可以看的黄网 | 99久久精品无码一区二区毛片 | 亚洲国产一区二区三区在线观看 | 国产美女拍拍拍在线观看 | 天天拍拍夜夜出水 | 久久久久久亚洲精品不卡 | 高清国产美女一级a毛片 | 另类视频一区 | 一区二区三区国产 | 成人影院一区二区三区 | 欧美一级艳片视频免费观看 | 在线一区免费播放 | 成人国产三级精品 | 中国国语毛片免费观看视频 | 国产盗摄视频 | 黄网在线 | 欧美激情精品久久久久久久九九九 | 久久99久久99精品观看 | 中文成人在线视频 |