基于Python實現(xiàn)一個簡易的數(shù)據(jù)管理系統(tǒng)
為了方便的實現(xiàn)記錄數(shù)據(jù)、修改數(shù)據(jù)沒有精力去做一個完整的系統(tǒng)去管理數(shù)據(jù)。因此,在python的控制臺直接實現(xiàn)一個簡易的數(shù)據(jù)管理系統(tǒng),包括數(shù)據(jù)的增刪改查等等。只需要在控制臺層面調(diào)用相應的功能調(diào)用查詢、修改等功能,這里記錄一下實現(xiàn)過程。


創(chuàng)建mysql數(shù)據(jù)表
使用比較熟悉的數(shù)據(jù)庫客戶端來進行操作,這里使用的是navicate客戶端來創(chuàng)建好相應的數(shù)據(jù)表。
創(chuàng)建數(shù)據(jù)庫并指定編碼字符集。
CREATE DATABASE `data_boc` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
創(chuàng)建數(shù)據(jù)記錄表boc
CREATE TABLE `boc` ( `id_` bigint(255) NOT NULL COMMENT '數(shù)據(jù)記錄編號,ID_作為主鍵', `boc_address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `boc_code` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `boc_email` varchar(24) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, `boc_name` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL, PRIMARY KEY (`id_`) USING BTREE )
增刪改查
import pymysql as mysql # 導入mysql驅(qū)動器 from pprint import pprint # 導入美觀的數(shù)據(jù)打印庫
確定一下需要實現(xiàn)哪些功能,在控制臺打印出功能列表,通過在控制臺輸入每個功能列表前面的標記來進入后臺系統(tǒng)的使用。
def current_menu():
'''
功能目錄列表展示
:return:
'''
pprint('---------------- 簡易數(shù)據(jù)管理系統(tǒng) ----------------')
pprint('系統(tǒng)功能實現(xiàn):<Python 集中營>')
pprint('1- 查詢數(shù)據(jù)列表')
pprint('2- 新增數(shù)據(jù)列表')
pprint('exit- 退出系統(tǒng)')
pprint('更多功能、暫未實現(xiàn)')
編寫數(shù)據(jù)庫連接的創(chuàng)建函數(shù),在修改或查詢數(shù)據(jù)時直接調(diào)用。
def cteate_connection():
'''
創(chuàng)建數(shù)據(jù)庫連接
:return:
'''
connection = mysql.connect(host='127.0.0.1',
user='root',
password='root',
database='data_boc')
return connection
編寫保存數(shù)據(jù)的函數(shù)用于數(shù)據(jù)列表新增功能實現(xiàn)。
def set_data():
'''
新增數(shù)據(jù)保存
:return:
'''
pprint('當前進入[2- 新增數(shù)據(jù)列表]')
id = input('輸入數(shù)據(jù)編號')
id = int(id)
boc_address = str(input('輸入詳細地址'))
boc_code = str(input('輸入具體編碼'))
boc_email = str(input('輸入正確郵箱'))
boc_name = str(input('輸入數(shù)據(jù)名稱'))
pprint('數(shù)據(jù)輸入完成,開始保存...')
'''創(chuàng)建數(shù)據(jù)庫接連'''
connection = cteate_connection()
cursor = connection.cursor()
insert_sql = "insert into boc(id_,boc_address,boc_code,boc_email,boc_name) values('%d','%s','%s',%s,%s)" % (
id, boc_address, boc_code, boc_email, boc_name)
try:
cursor.execute(insert_sql)
connection.commit()
except:
connection.rollback()
print("數(shù)據(jù)保存出現(xiàn)異常...")
connection.close()
pprint('數(shù)據(jù)保存完成...')
編寫數(shù)據(jù)列表的查詢功能函數(shù)。
def get_data():
pprint('當前進入[1- 查詢數(shù)據(jù)列表]')
'''創(chuàng)建數(shù)據(jù)庫連接'''
connection = cteate_connection()
cursor = connection.cursor()
select_sql = "select * from boc"
res_list = []
try:
cursor.execute(select_sql)
res = cursor.fetchall()
for row in res:
id = row[0]
boc_address = row[1]
boc_code = row[2]
boc_email = row[3]
boc_name = row[4]
res_list.append({'數(shù)據(jù)編號':id,'詳細地址':boc_address,'具體編碼':boc_code,'郵箱地址':boc_email,'名稱':boc_name})
pprint('數(shù)據(jù)結(jié)果:{}'.format(res_list))
connection.commit()
except:
print("數(shù)據(jù)查詢出現(xiàn)異常...")
connection.close()
pprint('數(shù)據(jù)查詢完成...')
啟動應用?
if __name__ == '__main__':
while True:
current_menu()
chiose_code = input('輸入菜單編號:')
if str(chiose_code) == '2':
set_data()
if str(chiose_code) == '1':
get_data()
if str(chiose_code) == 'exit':
break
到此這篇關(guān)于基于Python實現(xiàn)一個簡易的數(shù)據(jù)管理系統(tǒng)的文章就介紹到這了,更多相關(guān)Python數(shù)據(jù)管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分享python數(shù)據(jù)統(tǒng)計的一些小技巧
今天這些小技巧在處理python的一些數(shù)據(jù)方面還是很有幫助的,希望能幫到在這方面有需要的童鞋~2016-07-07
Linux環(huán)境下MySQL-python安裝過程分享
這篇文章主要介紹了Linux環(huán)境下MySQL-python安裝過程分享,本文使用的編譯方式安裝,需要的朋友可以參考下2015-02-02
Python利用Bokeh進行數(shù)據(jù)可視化的教程分享
Bokeh是Python中的數(shù)據(jù)可視化庫,提供高性能的交互式圖表和繪圖。本文將利用Bokeh繪制一些可視化圖表,文中的示例代碼講解詳細,感興趣的可以了解一下2022-08-08
python的scipy.stats模塊中正態(tài)分布常用函數(shù)總結(jié)
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python的scipy.stats模塊中正態(tài)分布常用函數(shù)總結(jié)內(nèi)容,有興趣的朋友們可以學習參考下。2021-02-02

