詳解MySQL?Shell?運行?SQL?的兩種內(nèi)置方法
MySQL Shell 是兼容 MySQL 傳統(tǒng)命令行客戶端的超級替代版,支持 SQL 、JavaScript 、Python 三種語言環(huán)境。工具自身包含了很多組件,使得 DBA 們管理 MySQL 更加便捷高效。
今天我們來介紹 MySQL Shell 的組件:MYSQLX 組件的兩個檢索函數(shù)在具體使用上的一些區(qū)別。
MYSQLX 組件包含很多預(yù)置的類庫, 其中與MySQL 交互最直接的就是 Session 類庫。Session 類庫里又包含一系列內(nèi)置函數(shù)來處理數(shù)據(jù):其中函數(shù) run_sql 和 sql 都可以直接和 MySQL 服務(wù)端交互來運行 SQL 語句。那到底有什么區(qū)別呢? 我們接下來具體介紹這兩個。(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 可以運行任何 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ù)的結(jié)果。
其實 run_sql 函數(shù)執(zhí)行后會返回一個 SqlResult 對象,SqlResult 對象包含很多函數(shù):獲取語句執(zhí)行時間,一次性獲取一行或者多行數(shù)據(jù),判斷是否有數(shù)據(jù)等等。 既然是 SqlResult ,那就是一個結(jié)果集,不支持多次獲取,類似 MySQL 的游標(biāo)。
接下來把 run_sql 函數(shù)執(zhí)行結(jié)果賦予一個變量 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í)行結(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 就變成一個 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í)行綁定變量的請求: 一次綁定一個數(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 運行 SQL 的兩種內(nèi)置方法概述的文章就介紹到這了,更多相關(guān)MySQL Shell 運行 SQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MySQL配置文件my.cnf中文詳解附mysql性能優(yōu)化方法分享
Mysql參數(shù)優(yōu)化對于新手來講,是比較難懂的東西,其實這個參數(shù)優(yōu)化,是個很復(fù)雜的東西,對于不同的網(wǎng)站,及其在線量,訪問量,帖子數(shù)量,網(wǎng)絡(luò)情況,以及機器硬件配置都有關(guān)系,優(yōu)化不可能一次性完成,需要不斷的觀察以及調(diào)試,才有可能得到最佳效果。2011-09-09
mysqldump數(shù)據(jù)庫備份參數(shù)詳解
這篇文章主要介紹了mysqldump數(shù)據(jù)庫備份參數(shù)詳解,需要的朋友可以參考下2014-05-05
MySQL計劃任務(wù)(事件調(diào)度器) Event Scheduler介紹
MySQL5.1.x版本中引入了一項新特性EVENT,顧名思義就是事件、定時任務(wù)機制,在指定的時間單元內(nèi)執(zhí)行特定的任務(wù),因此今后一些對數(shù)據(jù)定時性操作不再依賴外部程序,而直接使用數(shù)據(jù)庫本身提供的功能2013-10-10
mysql 中存在null和空時創(chuàng)建唯一索引的方法
據(jù)庫默認(rèn)值都有null,此時創(chuàng)建唯一索引時要注意了,此時數(shù)據(jù)庫會把空作為多個重復(fù)值2014-10-10
MySQL?移動數(shù)據(jù)目錄后啟動失敗問題解決
由于安裝數(shù)據(jù)庫時將MySQL的數(shù)據(jù)目錄放在了根目錄下,現(xiàn)在存儲空間不足,遇到這個問題如何解決呢,下面小編給大家?guī)砹薽ysql移動數(shù)據(jù)目錄啟動失敗解決方法,感興趣的朋友一起看看吧2023-04-04

