詳解MySQL?Shell?運行?SQL?的兩種內(nèi)置方法
MySQL Shell 是兼容 MySQL 傳統(tǒng)命令行客戶端的超級替代版,支持 SQL 、JavaScript 、Python 三種語言環(huán)境。工具自身包含了很多組件,使得 DBA 們管理 MySQL 更加便捷高效。
今天我們來介紹 MySQL Shell 的組件:MYSQLX 組件的兩個檢索函數(shù)在具體使用上的一些區(qū)別。
MYSQLX 組件包含很多預置的類庫, 其中與MySQL 交互最直接的就是 Session 類庫。Session 類庫里又包含一系列內(nèi)置函數(shù)來處理數(shù)據(jù):其中函數(shù) run_sql 和 sql 都可以直接和 MySQL 服務端交互來運行 SQL 語句。那到底有什么區(qū)別呢? 我們接下來具體介紹這兩個。(Python 環(huán)境寫法:run_sql、sql;JavaScript環(huán)境下:runSQL、sql)
第一、函數(shù)run_sql 如何使用:
先連上 X 端口 33060,替代默認語言環(huán)境為 Python ,變量 c1 即為 Session 對象(Session:root@localhost:33060)。
root@ytt-pc-cheap:/home/ytt# mysqlsh mysqlx:/root@localhost:33060/ytt --py MySQL Shell 8.0.30 ... Creating an X protocol session to 'root@localhost:33060/ytt' Fetching schema names for autocompletion... Press ^C to stop. Your MySQL connection id is 9 (X protocol) Server version: 8.0.30 MySQL Community Server - GPL Default schema `ytt` accessible through db. MySQL localhost:33060+ ssl ytt Py > c1=db.get_session() MySQL localhost:33060+ ssl ytt Py > c1 <Session:root@localhost:33060>
執(zhí)行 run_sql 創(chuàng)建表t1: run_sql 可以運行任何 MySQL 兼容的 SQL 語句。
MySQL localhost:33060+ ssl ytt Py > c1.run_sql("create table t1(id int auto_increment primary key, r1 int)") Query OK, 0 rows affected (0.0656 sec) MySQL localhost:33060+ ssl ytt Py > c1.run_sql("desc t1") +-------+------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | r1 | int | YES | | NULL | | +-------+------+------+-----+---------+----------------+ 2 rows in set (0.0017 sec)
插入幾條樣例數(shù)據(jù):
MySQL localhost:33060+ ssl ytt Py > c1.run_sql("insert into t1(r1) values (10),(20),(30)") Query OK, 3 rows affected (0.0114 sec) Records: 3 Duplicates: 0 Warnings: 0
用 run_sql 來執(zhí)行 查詢語句:
MySQL localhost:33060+ ssl ytt Py > c1.run_sql("table t1") +----+----+ | id | r1 | +----+----+ | 1 | 10 | | 2 | 20 | | 3 | 30 | +----+----+ 3 rows in set (0.0008 sec)
以上都是直接運行 run_sql 函數(shù)的結果。
其實 run_sql 函數(shù)執(zhí)行后會返回一個 SqlResult 對象,SqlResult 對象包含很多函數(shù):獲取語句執(zhí)行時間,一次性獲取一行或者多行數(shù)據(jù),判斷是否有數(shù)據(jù)等等。 既然是 SqlResult ,那就是一個結果集,不支持多次獲取,類似 MySQL 的游標。
接下來把 run_sql 函數(shù)執(zhí)行結果賦予一個變量 r1 ,后續(xù)操作都通過 r1 來進行:r1 被賦予 SqlResult 對象。
MySQL localhost:33060+ ssl ytt Py > r1=c1.run_sql("table t1") MySQL localhost:33060+ ssl ytt Py > r1.has_data() true MySQL localhost:33060+ ssl ytt Py > r1.get_execution_time() 0.0010 sec MySQL localhost:33060+ ssl ytt Py > r1.fetch_one() [ 1, 10 ] MySQL localhost:33060+ ssl ytt Py > r1.fetch_one() [ 2, 20 ] MySQL localhost:33060+ ssl ytt Py > r1.fetch_one() [ 3, 30 ] MySQL localhost:33060+ ssl ytt Py > r1.fetch_one() MySQL localhost:33060+ ssl ytt Py >
run_sql 函數(shù)也可以綁定變量執(zhí)行:
MySQL localhost:33060+ ssl ytt Py > c1.run_sql("select * from t1 where r1 in (?,?,?)",[10,20,30]) +----+----+ | id | r1 | +----+----+ | 1 | 10 | | 2 | 20 | | 3 | 30 | +----+----+ 3 rows in set (0.0004 sec)
第二、函數(shù) sql 如何使用:
sql 函數(shù)和 run_sql 函數(shù)不一樣,它返回的不是 SqlResult 對象,而是一個 SqlExecute 對象,是 SqlResult 對象產(chǎn)生之前的階段。舉個例子:把 sql 函數(shù)執(zhí)行結果賦予變量 r2 ,這樣每調用一次 r2 ,相當于重新執(zhí)行一次原請求。
MySQL localhost:33060+ ssl ytt Py > r2=c1.sql("table t1") MySQL localhost:33060+ ssl ytt Py > r2 +----+----+ | id | r1 | +----+----+ | 1 | 10 | | 2 | 20 | | 3 | 30 | +----+----+ 3 rows in set (0.0004 sec) MySQL localhost:33060+ ssl ytt Py > r2 +----+----+ | id | r1 | +----+----+ | 1 | 10 | | 2 | 20 | | 3 | 30 | +----+----+ 3 rows in set (0.0002 sec)
如果把變量 r2 的執(zhí)行結果賦予變量 r3 ,那 r3 就變成一個 SqlResult 對象,只支持獲取一次,又回退到 run_sql 函數(shù)的結果:
MySQL localhost:33060+ ssl ytt Py > r3=r2.execute() MySQL localhost:33060+ ssl ytt Py > r3.fetch_all() [ [ 1, 10 ], [ 2, 20 ], [ 3, 30 ] ] MySQL localhost:33060+ ssl ytt Py > r3.fetch_all() [] MySQL localhost:33060+ ssl ytt Py > r3 Empty set (0.0004 sec)
sql 函數(shù)同樣支持執(zhí)行綁定變量的請求: 一次綁定一個數(shù)組。
MySQL localhost:33060+ ssl ytt Py > r2=c1.sql("select * from t1 where r1 in (?,?,?)") MySQL localhost:33060+ ssl ytt Py > r2.bind([10,20,30]) +----+----+ | id | r1 | +----+----+ | 1 | 10 | | 2 | 20 | | 3 | 30 | +----+----+ 3 rows in set (0.0006 sec) MySQL localhost:33060+ ssl ytt Py > r2.bind([40,50,30]) +----+----+ | id | r1 | +----+----+ | 3 | 30 | +----+----+ 1 row in set (0.0002 sec)
結論:
對于函數(shù) run_sql 和 sql 來講,可以參考對象 SqlResult 和 SqlExecute 的差異來選擇自己最合適的使用場景。
到此這篇關于MySQL Shell 運行 SQL 的兩種內(nèi)置方法概述的文章就介紹到這了,更多相關MySQL Shell 運行 SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MySQL無法啟動、無法停止解決方法(安全設置后容易出現(xiàn))
最近在Win2003上的MySQL出現(xiàn)過多次正常運行時無法連接數(shù)據(jù)庫故障,根本原因就是因為安全設置以后容易出現(xiàn)的問題,其實很簡單的解決2012-03-03IDEA連接MySQL數(shù)據(jù)庫并執(zhí)行SQL語句使用數(shù)據(jù)圖文詳解
使用idea連接本地MySQL數(shù)據(jù)庫,就可以很方便的看到數(shù)據(jù)庫的內(nèi)容,還可以進行基本的增加,刪除,修改操作,下面這篇文章主要給大家介紹了關于IDEA連接MySQL數(shù)據(jù)庫并執(zhí)行SQL語句使用數(shù)據(jù)的相關資料,需要的朋友可以參考下2023-03-03解析:內(nèi)聯(lián),左外聯(lián),右外聯(lián),全連接,交叉連接的區(qū)別
本篇文章是對內(nèi)聯(lián),左外聯(lián),右外聯(lián),全連接,交叉連接的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-07-07Navicat連接MySQL提示1045錯誤解決(重置MySQL密碼)
連接MySQL數(shù)據(jù)庫時難免會遇到1045錯誤,主要是因為用戶輸入的用戶名或密碼錯誤被拒絕訪問,如果不想重裝,需要找回密碼或者重置密碼,這篇文章主要給大家介紹了關于Navicat連接MySQL提示1045錯誤解決的方法,主要是重置MySQL密碼,需要的朋友可以參考下2023-04-04Windows10下mysql 8.0.22 安裝配置方法圖文教程
這篇文章主要為大家詳細介紹了Windows10下mysql 8.0.22 安裝配置方法圖文教程,文中安裝步驟介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11