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

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

python 實(shí)現(xiàn)數(shù)據(jù)庫(kù)中數(shù)據(jù)添加、查詢與更新的示例代碼

瀏覽:102日期:2022-07-02 18:38:29

一、前言

最近做web網(wǎng)站的測(cè)試,遇到很多需要批量造數(shù)據(jù)的功能;比如某個(gè)頁(yè)面展示數(shù)據(jù)條數(shù)需要達(dá)到10000條進(jìn)行測(cè)試,此時(shí)手動(dòng)構(gòu)造數(shù)據(jù)肯定是不可能的,此時(shí)只能通過(guò)python腳本進(jìn)行自動(dòng)構(gòu)造數(shù)據(jù);本次構(gòu)造數(shù)據(jù)主要涉及到在某個(gè)表里面批量添加數(shù)據(jù)、在關(guān)聯(lián)的幾個(gè)表中同步批量添加數(shù)據(jù)、批量查詢某個(gè)表中符合條件的數(shù)據(jù)、批量更新某個(gè)表中符合條件的數(shù)據(jù)等。

二、數(shù)據(jù)添加

即批量添加數(shù)據(jù)到某個(gè)表中。

insert_data.py

import pymysqlimport randomimport timefrom get_userinfo import get_userinfofrom get_info import get_infofrom get_tags import get_tagsfrom get_tuser_id import get_utagclass DatabaseAccess(): def __init__(self): self.__db_host = 'xxxxx' self.__db_port = 3307 self.__db_user = 'root' self.__db_password = '123456' self.__db_database = 'xxxxxx' # 連接數(shù)據(jù)庫(kù) def isConnectionOpen(self): self.__db = pymysql.connect( host=self.__db_host, port=self.__db_port, user=self.__db_user, password=self.__db_password, database=self.__db_database, charset=’utf8’ ) # 插入數(shù)據(jù) def linesinsert(self,n,user_id,tags_id,created_at): self.isConnectionOpen() # 創(chuàng)建游標(biāo) global cursor conn = self.__db.cursor() try: sql1 = ’’’ INSERT INTO `codeforge_new`.`cf_user_tag`(`id`, `user_id`, `tag_id`, `created_at`, `updated_at`) VALUES ({}, {}, {}, ’{}’, ’{}’); ’’’.format(n,user_id,tags_id,created_at,created_at) # 執(zhí)行SQLconn.execute(sql1,) except Exception as e: print(e) finally: # 關(guān)閉游標(biāo) conn.close() self.__db.commit() self.__db.close() def get_data(self):# 生成對(duì)應(yīng)數(shù)據(jù) 1000條 for i in range(0,1001): created_at = time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime()) # print(create_at) # 用戶id tuserids = [] tuserid_list = get_utag() for tuserid in tuserid_list:tuserids.append(tuserid[0]) # print(tuserids) userid_list = get_userinfo() user_id = random.choice(userid_list)[0] if user_id not in tuserids:user_id=user_id # 標(biāo)簽idtagsid_list = get_tags()tags_id = random.choice(tagsid_list)[0]self.linesinsert(i,user_id,tags_id,created_at)if __name__ == '__main__': # 實(shí)例化對(duì)象 db=DatabaseAccess() db.get_data()

二、數(shù)據(jù)批量查詢

select_data.py

import pymysqlimport pandas as pdimport numpy as npdef get_tags(): # 連接數(shù)據(jù)庫(kù),地址,端口,用戶名,密碼,數(shù)據(jù)庫(kù)名稱,數(shù)據(jù)格式 conn = pymysql.connect(host=’xxx.xxx.xxx.xxx’,port=3307,user=’root’,passwd=’123456’,db=’xxxx’,charset=’utf8’) cur = conn.cursor() # 表cf_users中獲取所有用戶id sql = ’select id from cf_tags where id between 204 and 298’ # 將user_id列轉(zhuǎn)成列表輸出 df = pd.read_sql(sql,con=conn) # 先使用array()將DataFrame轉(zhuǎn)換一下 df1 = np.array(df) # 再將轉(zhuǎn)換后的數(shù)據(jù)用tolist()轉(zhuǎn)成列表 df2 = df1.tolist() # cur.execute(sql) # data = cur.fetchone() # print(df) # print(df1) # print(df2) return df2 conn.close()

三、批量更新數(shù)據(jù)

select_data.py

import pymysqlimport pandas as pdimport numpy as npdef get_tags(): # 連接數(shù)據(jù)庫(kù),地址,端口,用戶名,密碼,數(shù)據(jù)庫(kù)名稱,數(shù)據(jù)格式 conn = pymysql.connect(host=’xxx.xxx.xxx.xxx’,port=3307,user=’root’,passwd=’123456’,db=’xxxx’,charset=’utf8’) cur = conn.cursor() # 表cf_users中獲取所有用戶id sql = ’select id from cf_tags where id between 204 and 298’ # 將user_id列轉(zhuǎn)成列表輸出 df = pd.read_sql(sql,con=conn) # 先使用array()將DataFrame轉(zhuǎn)換一下 df1 = np.array(df) # 再將轉(zhuǎn)換后的數(shù)據(jù)用tolist()轉(zhuǎn)成列表 df2 = df1.tolist() # cur.execute(sql) # data = cur.fetchone() # print(df) # print(df1) # print(df2) return df2 conn.close()

以上就是python 實(shí)現(xiàn)數(shù)據(jù)庫(kù)中數(shù)據(jù)添加、查詢與更新的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 數(shù)據(jù)庫(kù)添加、查詢與更新的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 网红主播大尺度精品福利视频 | 精品国产免费人成在线观看 | 国产日本韩国不卡在线视频 | 欧美精品综合一区二区三区 | 久久久久久免费观看 | 在线第一页 | 91精品欧美一区二区三区 | 国产成人免费不卡在线观看 | 男女在线免费视频 | 国产区精品 | 99精品福利 | 亚洲国产精品线在线观看 | 国产成人aa在线视频 | 国产a级三级三级三级 | 日本在线网 | 国产亚洲精品一区二区久久 | a级片在线观看视频 | 国产免费v片在线看 | 久久99精品久久久久久秒播放器 | 日本一级毛片高清免费观看视频 | 国产毛片一级国语版 | 悟空影视大全免费高清 | 午夜美女久久久久爽久久 | 天堂中文资源在线8 | 亚洲美女一级片 | 亚洲在线观看 | 一级毛片子 | 久久成人免费观看全部免费 | 午夜日韩| 国产三级日产三级韩国三级 | 欧美一级特黄aa大片视频 | 91亚洲精品 | 在线看片 在线播放 | 亚洲精选在线观看 | 99久久精品免费观看国产 | 日韩精品一区二区三区高清 | 中文字幕在线播放 | 欧美一级带 | 一区二区三区成人 | 欧美精品一级毛片 | 精品在线观看免费 |