詳解MySQL?Shell?運(yùn)行?SQL?的兩種內(nèi)置方法
MySQL Shell 是兼容 MySQL 傳統(tǒng)命令行客戶端的超級替代版,支持 SQL 、JavaScript 、Python 三種語言環(huán)境。工具自身包含了很多組件,使得 DBA 們管理 MySQL 更加便捷高效。
今天我們來介紹 MySQL Shell 的組件:MYSQLX 組件的兩個(gè)檢索函數(shù)在具體使用上的一些區(qū)別。
MYSQLX 組件包含很多預(yù)置的類庫, 其中與MySQL 交互最直接的就是 Session 類庫。Session 類庫里又包含一系列內(nèi)置函數(shù)來處理數(shù)據(jù):其中函數(shù) run_sql 和 sql 都可以直接和 MySQL 服務(wù)端交互來運(yùn)行 SQL 語句。那到底有什么區(qū)別呢? 我們接下來具體介紹這兩個(gè)。(Python 環(huán)境寫法:run_sql、sql;JavaScript環(huán)境下:runSQL、sql)
第一、函數(shù)run_sql 如何使用:
先連上 X 端口 33060,替代默認(rèn)語言環(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 可以運(yùn)行任何 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)
以上都是直接運(yùn)行 run_sql 函數(shù)的結(jié)果。
其實(shí) run_sql 函數(shù)執(zhí)行后會(huì)返回一個(gè) SqlResult 對象,SqlResult 對象包含很多函數(shù):獲取語句執(zhí)行時(shí)間,一次性獲取一行或者多行數(shù)據(jù),判斷是否有數(shù)據(jù)等等。 既然是 SqlResult ,那就是一個(gè)結(jié)果集,不支持多次獲取,類似 MySQL 的游標(biāo)。
接下來把 run_sql 函數(shù)執(zhí)行結(jié)果賦予一個(gè)變量 r1 ,后續(xù)操作都通過 r1 來進(jìn)行: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 對象,而是一個(gè) SqlExecute 對象,是 SqlResult 對象產(chǎn)生之前的階段。舉個(gè)例子:把 sql 函數(shù)執(zhí)行結(jié)果賦予變量 r2 ,這樣每調(diào)用一次 r2 ,相當(dāng)于重新執(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í)行結(jié)果賦予變量 r3 ,那 r3 就變成一個(gè) SqlResult 對象,只支持獲取一次,又回退到 run_sql 函數(shù)的結(jié)果:
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í)行綁定變量的請求: 一次綁定一個(gè)數(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)
結(jié)論:
對于函數(shù) run_sql 和 sql 來講,可以參考對象 SqlResult 和 SqlExecute 的差異來選擇自己最合適的使用場景。
到此這篇關(guān)于MySQL Shell 運(yùn)行 SQL 的兩種內(nèi)置方法概述的文章就介紹到這了,更多相關(guān)MySQL Shell 運(yùn)行 SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用PowerShell批量導(dǎo)出MySQL數(shù)據(jù)庫的新手教程
- MySQL備份Shell腳本的實(shí)現(xiàn)
- 使用Shell腳本進(jìn)行MySql權(quán)限修改的實(shí)現(xiàn)教程
- 在Centos7中利用Shell腳本實(shí)現(xiàn)MySQL數(shù)據(jù)備份
- mysql數(shù)據(jù)庫中g(shù)etshell的方式總結(jié)
- 教你使用MySQL Shell連接數(shù)據(jù)庫的方法
- 利用MySQL?Shell安裝部署MGR集群的詳細(xì)過程
- shell腳本命令搞定mysql_exporter部署,重復(fù)配置自動(dòng)化
相關(guān)文章
解決Win10系統(tǒng)安裝MySQL8.0遇到的問題
這篇文章主要介紹了Win10系統(tǒng)安裝MySQL8.0遇到的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-12-12
MySQL日志專項(xiàng)之redo log和undo log介紹
MySQL日志記錄了MySQL數(shù)據(jù)庫日常操作和錯(cuò)誤信息,MySQL有不同類型的日志文件(各自存儲(chǔ)了不同類型的日志),從日志當(dāng)中可以查詢到MySQL數(shù)據(jù)庫的運(yùn)行情況、用戶操作、錯(cuò)誤信息等2022-08-08
MySQL mysql-connector的具體實(shí)現(xiàn)
本文主要介紹了MySQL mysql-connector的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11
mysql5.6 解析JSON字符串方式(支持復(fù)雜的嵌套格式)
這篇文章主要介紹了mysql5.6 解析JSON字符串方式(支持復(fù)雜的嵌套格式),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
解決創(chuàng)建主鍵報(bào)錯(cuò):Incorrect column specifier for
這篇文章主要介紹了解決創(chuàng)建主鍵報(bào)錯(cuò):Incorrect column specifier for column‘id‘問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

