最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年12月07日 16:14:27   作者:咖啡少女不加糖。  
這篇文章主要介紹了python 實(shí)現(xiàn)數(shù)據(jù)庫(kù)中數(shù)據(jù)添加、查詢與更新的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一、前言

  最近做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ù)等?! ?/p>

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

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

insert_data.py

import pymysql
import random
import time
from get_userinfo import get_userinfo
from get_info import get_info
from get_tags import get_tags
from get_tuser_id import get_utag


class 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í)行SQL  
      conn.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)簽id
        tagsid_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 pymysql
import pandas as pd
import numpy as np


def 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 pymysql
import pandas as pd
import numpy as np


def 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)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python實(shí)現(xiàn)計(jì)算資源圖標(biāo)crc值的方法

    python實(shí)現(xiàn)計(jì)算資源圖標(biāo)crc值的方法

    這篇文章主要介紹了python實(shí)現(xiàn)計(jì)算資源圖標(biāo)crc值的方法,通過(guò)解析資源文件找到icon的數(shù)據(jù),從而實(shí)現(xiàn)該功能,需要的朋友可以參考下
    2014-10-10
  • Python hashlib和hmac模塊使用方法解析

    Python hashlib和hmac模塊使用方法解析

    這篇文章主要介紹了Python hashlib和hmac模塊使用方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • django orm模塊中的 is_delete用法

    django orm模塊中的 is_delete用法

    這篇文章主要介紹了django orm模塊中的 is_delete用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • python3爬蟲(chóng)怎樣構(gòu)建請(qǐng)求header

    python3爬蟲(chóng)怎樣構(gòu)建請(qǐng)求header

    在本篇內(nèi)容里小編給大家分享了關(guān)于python3爬蟲(chóng)怎樣構(gòu)建請(qǐng)求header的知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2018-12-12
  • Python matplotlib如何繪制各種流線圖

    Python matplotlib如何繪制各種流線圖

    在Python中不僅可以繪制折線圖、柱狀圖、散點(diǎn)圖等常規(guī)圖外,還支持繪制量場(chǎng)圖、頻譜圖、提琴圖、箱型圖等特殊圖。本文將主要介紹如何繪制流線圖,需要的朋友可以參考一下
    2021-12-12
  • Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

    Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn)

    這篇文章主要介紹了Python接口自動(dòng)化測(cè)試的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • python 解決數(shù)據(jù)庫(kù)寫(xiě)入時(shí)float自動(dòng)變?yōu)檎麛?shù)的問(wèn)題

    python 解決數(shù)據(jù)庫(kù)寫(xiě)入時(shí)float自動(dòng)變?yōu)檎麛?shù)的問(wèn)題

    這篇文章主要介紹了python 解決數(shù)據(jù)庫(kù)寫(xiě)入時(shí)float自動(dòng)變?yōu)檎麛?shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Python比較2個(gè)時(shí)間大小的實(shí)現(xiàn)方法

    Python比較2個(gè)時(shí)間大小的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇Python比較2個(gè)時(shí)間大小的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • python向量化與for循環(huán)耗時(shí)對(duì)比分析

    python向量化與for循環(huán)耗時(shí)對(duì)比分析

    這篇文章主要介紹了python向量化與for循環(huán)耗時(shí)對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 對(duì)字典按照value進(jìn)行排序的方法

    python 對(duì)字典按照value進(jìn)行排序的方法

    這篇文章主要介紹了python 對(duì)字典按照value進(jìn)行排序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論

崇信县| 巩义市| 定州市| 鞍山市| 沅江市| 泗水县| 保康县| 太和县| 肥乡县| 梧州市| 襄樊市| 云南省| 榆林市| 句容市| 宝丰县| 望城县| 烟台市| 新郑市| 偃师市| 康平县| 桦甸市| 阿拉善右旗| 葫芦岛市| 蓬莱市| 海伦市| 刚察县| 兰州市| 武邑县| 德江县| 临沭县| 普兰店市| 吴旗县| 鄂温| 秭归县| 昌黎县| 揭东县| 铜山县| 镇平县| 穆棱市| 全州县| 洛隆县|