中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

wordpress判斷是否登錄英文seo兼職

wordpress判斷是否登錄,英文seo兼職,如何給網(wǎng)站做優(yōu)化,湖南智慧住建云官網(wǎng)文章目錄 一、read_sql()二、to_sql()三、連接數(shù)據(jù)庫方式—MySQL1、用sqlalchemy包構(gòu)建數(shù)據(jù)庫鏈接2、用DBAPI構(gòu)建數(shù)據(jù)庫鏈接 四、容易遇到的問題 一、read_sql() 功能 將 SQL 查詢/數(shù)據(jù)庫表讀入 DataFrame。 語法 讀取數(shù)據(jù)庫(通過SQL語句或表名) pand…

文章目錄

  • 一、read_sql()
  • 二、to_sql()
  • 三、連接數(shù)據(jù)庫方式—MySQL
    • 1、用sqlalchemy包構(gòu)建數(shù)據(jù)庫鏈接
    • 2、用DBAPI構(gòu)建數(shù)據(jù)庫鏈接
  • 四、容易遇到的問題


一、read_sql()

  • 功能

將 SQL 查詢/數(shù)據(jù)庫表讀入 DataFrame。

  • 語法

    • 讀取數(shù)據(jù)庫(通過SQL語句或表名)

pandas.read_sql(sql, con, index_col: ‘str | Sequence[str] | None’ = None, coerce_float: ‘bool’ = True, params=None, parse_dates=None, columns=None, chunksize: ‘int | None’ = None) -> ‘DataFrame | Iterator[DataFrame]’

    • 讀取自定義數(shù)據(jù)(通過SQL語句)

pandas.read_sql_query(sql, con, index_col=None, coerce_float: ‘bool’ = True, params=None, parse_dates=None, chunksize: ‘int | None’ = None, dtype: ‘DtypeArg | None’ = None) -> ‘DataFrame | Iterator[DataFrame]’

    • 讀取整張表于DataFrame格式(通過表名)

pandas.read_sql_table(table_name: ‘str’, con, schema: ‘str | None’ = None, index_col: ‘str | Sequence[str] | None’ = None, coerce_float: ‘bool’ = True, parse_dates=None, columns=None, chunksize: ‘int | None’ = None) -> ‘DataFrame | Iterator[DataFrame]’

read_sql是綜合了read_sql_table和read_sql_query的,所以一般用read_sql就好了。

  • 基本參數(shù)
名稱說明
sql要執(zhí)行的數(shù)據(jù)庫或SQL命令字符串。
con連接sql數(shù)據(jù)庫的engine,一般可以用SQLalchemy或者pymysql之類的包建立。
index_col選擇某1列或幾列作為index(或MultiIndex),字符串或字符串列表。
coerce_float布爾值,將數(shù)字形式的字符串直接以float型讀入,默認為True。
params執(zhí)行查詢時傳遞的參數(shù)。
parse_dates將某一列日期型字符串轉(zhuǎn)換為datetime型數(shù)據(jù),與pd.to_datetime函數(shù)功能類似??梢灾苯犹峁┬枰D(zhuǎn)換的列名以默認的日期形式轉(zhuǎn)換,也可以用字典的格式提供列名和轉(zhuǎn)換的日期格式:比如{column_name: format string}(format string:“%Y:%m:%H:%M:%S”)。
columns要選取的列。一般沒啥用,因為在sql命令里面一般就指定要選擇的列了。
chunksize如果提供了一個整數(shù)值,那么就會返回一個generator,每次輸出的行數(shù)就是提供的值的大小。

二、to_sql()

  • 功能

將DataFrame寫入SQL數(shù)據(jù)庫表。

  • 語法

to_sql(name: ‘str’, con, schema=None, if_exists: ‘str’ = ‘fail’, index: ‘bool_t’ = True, index_label=None, chunksize=None, dtype: ‘DtypeArg | None’ = None, method=None) -> ‘int | None’ method of pandas.core.frame.DataFrame instance

  • 基本參數(shù)
名稱說明
name數(shù)據(jù)庫對應的表名
con與數(shù)據(jù)庫鏈接的方式,推薦使用sqlalchemy的engine類型
schema相應數(shù)據(jù)庫的引擎,不設置則使用數(shù)據(jù)庫的默認引擎,如mysql中的innodb引擎
if_exists可選參數(shù),字符串,默認是"fail"。當數(shù)據(jù)庫中已經(jīng)存在數(shù)據(jù)表時,對數(shù)據(jù)表的操作,有replace替換,即刪除原來的表,重新創(chuàng)建一個新表;append追加,fail則當表存在時提示ValueError。
index可選參數(shù),bool類型,默認是True。是否將DataFrame的索引寫入數(shù)據(jù)庫表中。
index_label可選參數(shù),字符串類型,當上一個參數(shù)index為True時,設置寫入數(shù)據(jù)表時index的列名稱。
chunksize可選參數(shù),int類型,默認是None。一次寫入數(shù)據(jù)時的數(shù)據(jù)行數(shù)量,設置整數(shù),如20000,當數(shù)據(jù)量很大時,需要設置,否則會鏈接超時寫入失敗。
dtype可選參數(shù),字典類型,默認是None。將列名映射到SQL類型。

三、連接數(shù)據(jù)庫方式—MySQL

1、用sqlalchemy包構(gòu)建數(shù)據(jù)庫鏈接

通過 sqlalchemy 的 create_engine 創(chuàng)建:有兩種方式,基本格式一致,區(qū)別只是在于使用 mysqldb,還是使用 pymysql,推薦使用pymysql。

pymysql使用方式

import pandas as pd
from sqlalchemy import create_engine# 建立數(shù)據(jù)庫連接
# "mysql+pymysql://{用戶名}:{密碼}@{域名}:{端口號}/{數(shù)據(jù)庫名}"
engine = create_engine("mysql+pymysql://root:123456@localhost:3306/data")

mysqldb使用方式

mysqldb 是 python2 的 mysql 連接庫,在 python3 時,已經(jīng)廢除 mysqldb,改為pymysql。在 sqlachemy 必須使用 mysqldb 驅(qū)動時,需要先導入pymysql ,然后執(zhí)行 “pymysql.install_as_MySQLdb()” 才能使用。

import pandas as pd
import pymysql
from sqlalchemy import create_engine
pymysql.install_as_MySQLdb()
engine = create_engine("mysql+pymysql://root:123456@localhost:3306/data")

封裝數(shù)據(jù)庫信息,格式化傳入:

db_info = {'user':'root','password':'123456','host':'localhost','database':'data','port':3306
}
engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s:%(port)d/%(database)s?charset=utf8' % db_info)

示例1: 將 MySQL數(shù)據(jù)庫表讀入 DataFrame 。

# 定義SQL查詢
sql_query = 'select * from sc'# 執(zhí)行查詢操作:把sql查詢結(jié)果讀取為dataframe
df = pd.read_sql(sql_query,engine)

示例2: 將 DataFrame 中的數(shù)據(jù)寫入 MySQL 數(shù)據(jù)庫表。

# 執(zhí)行寫入操作:將dataframe寫入sql數(shù)據(jù)表
df.to_sql(name='',con=engine,if_exists='replace',index=False)

2、用DBAPI構(gòu)建數(shù)據(jù)庫鏈接

import pandas as pd
import pymysql# 建立數(shù)據(jù)庫連接
conn = pymysql.connect(host='localhost',		# 主機名(或IP地址)port=3306,				# 端口號,默認為3306user='root',			# 用戶名password='123456',	# 密碼charset='utf8'  		# 設置字符編碼
)
# 獲取mysql服務信息(測試連接,會輸出MySQL版本號)
print(conn.get_server_info())

示例1: 將 MySQL數(shù)據(jù)庫表讀入 DataFrame 。

# 選擇數(shù)據(jù)庫
conn.select_db("database")# 定義SQL查詢
sql_query = 'select * from sc'# 執(zhí)行查詢操作:把sql查詢結(jié)果讀取為dataframe
df = pd.read_sql(sql_query,conn)

示例2: 將 DataFrame 中的數(shù)據(jù)寫入 MySQL數(shù)據(jù)庫表。

# 執(zhí)行寫入操作:將dataframe寫入sql數(shù)據(jù)表
df.to_sql(name='',con=conn ,if_exists='replace',index=False)

會顯示下面的報錯情況:
TypeError: not all arguments converted during string formatting

pandas.errors.DatabaseError: Execution failed on sql ' SELECT name FROM sqlite_master WHERE type IN ('table', 'view') AND name=?; ': not all arguments converted during string formatting

在這里插入圖片描述

原因: 引擎問題。在python3中,to_sql() 的con對象,是 sqlalchemy 的 engine 引擎。

解決方案: 使用to_sql()將dataframe寫入sql數(shù)據(jù)表,要用sqlalchemy包構(gòu)建數(shù)據(jù)庫鏈接。參考文章:https://blog.csdn.net/xiaoyw71/article/details/131126161。

四、容易遇到的問題

問題一: python中sqlalchemy操作mysql密碼包含@特殊字符。

import pandas as pd
from sqlalchemy import create_enginedb_info = {'user':'root','password':'123@456','host':'localhost','database':'data','port':3306
}
engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s:%(port)d/%(database)s?charset=utf8' % db_info)# 定義SQL查詢
sql_query = 'select * from sc'# 執(zhí)行SQL查詢操作
df=pd.read_sql_query(sql_query ,engine)

報錯:sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '2024jyz@172.30.21.57' ([Errno -2] Name or service not known)")

在這里插入圖片描述

解決方案:

import pandas as pd
from sqlalchemy import create_engine
from urllib import parsedb_info = {'user':'root','password':parse.quote_plus('123@456'),'host':'localhost','database':'data','port':3306
}engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s:%(port)d/%(database)s?charset=utf8' % db_info)

參考文章:
https://blog.csdn.net/initiallht/article/details/120406317
https://blog.csdn.net/qq_41982570/article/details/127059642


參考文章:
https://blog.csdn.net/LeiLiFengX/article/details/109922043
https://www.cnblogs.com/think90/articles/11899070.html

http://www.risenshineclean.com/news/37157.html

相關文章:

  • 找設計案例的網(wǎng)站接推廣app任務的平臺
  • html編輯器在哪里設置seo是哪個國家
  • jsp網(wǎng)站首頁那欄怎么做南平seo
  • 做網(wǎng)站開發(fā)哪里好網(wǎng)站收錄一鍵提交
  • php做的大型網(wǎng)站百度搜索引擎推廣步驟
  • h5網(wǎng)站建設機構(gòu)阿里云模板建站
  • 網(wǎng)站開發(fā)發(fā)送短信seo網(wǎng)站推廣工作內(nèi)容
  • 哪些是大型網(wǎng)站友情鏈接樣式
  • 網(wǎng)站引導頁是什么問題新產(chǎn)品推廣方案策劃
  • 東莞企石網(wǎng)站建設南寧百度推廣代理公司
  • 汕頭高端網(wǎng)站開發(fā)什么是域名
  • 網(wǎng)站在線備案太原seo全網(wǎng)營銷
  • 建設一個菠菜網(wǎng)站成本我贏網(wǎng)seo優(yōu)化網(wǎng)站
  • 怎樣在微信中做網(wǎng)站六六seo基礎運營第三講
  • 做老師好還是網(wǎng)站編輯好常見的系統(tǒng)優(yōu)化軟件
  • 域名過期做的網(wǎng)站怎么辦全國十大跨境電商排名
  • Wordpress主題 仿魅族青島seo杭州廠商
  • 如何做淘客發(fā)單網(wǎng)站海外推廣
  • 怎樣做醫(yī)院網(wǎng)站做電商一個月能掙多少錢
  • 產(chǎn)品開發(fā)的流程seo外包公司多少錢
  • 網(wǎng)站排名不可有利就前正規(guī)網(wǎng)絡推廣服務
  • tp框架做網(wǎng)站的優(yōu)點seo網(wǎng)站排名助手
  • 招聘網(wǎng)站開發(fā)流程自動搜索關鍵詞軟件
  • 畢業(yè)網(wǎng)站設計代做友情鏈接外鏈
  • 優(yōu)惠券網(wǎng)站要怎么做的朋友圈推廣廣告
  • 品牌建設的四條主線seo頁面優(yōu)化技術
  • 做網(wǎng)站特別注意什么百度網(wǎng)盤app怎么打開鏈接
  • 外貿(mào)網(wǎng)站制作百度純凈版首頁入口
  • 住房和城鄉(xiāng)建設部網(wǎng)站買賣合同seo網(wǎng)站推廣經(jīng)理招聘
  • 做外貿(mào)翻譯用哪個網(wǎng)站網(wǎng)站建設公司業(yè)務