mysql execute指令 mysql常用命令

2021-10-18 09:14:01 字數 2251 閱讀 7546

一、游標

游標(cursor)是處理資料的一種方法,為了檢視或者處理結果集中的資料,游標提供了在結果集中一次一行或者多行前進或向後瀏覽資料的能力。可以把游標當作乙個指標,它可以指定結果中的任何位置,然後允許使用者對指定位置的資料進行處理。

模版案例一、

import…………

if __name__ == '__main__':

cnx = connect_mysql()

cus = cnx.cursor()

sql = ''' create table test(id int not null);insert into test(id) values (100);'''

try:

cus.execute(sql)

cus.close()

cnx.commit()

except exception as e:

cnx.rollback()

print('error')

# raise e

finally:

cnx.close()

案例二如下:

import pymysql

# 建立連線

conn = pymysql.connect(host="192.168.48.136", port=3306, user="xiang", passwd="xiang", db="test")

# 建立游標

cus = conn.cursor()

# 定義sql

sql = "select * from test2;"

# 執行

# cus.execute(sql)

# 取所有的結果,取結果之前,一定要先執行sql

# cus.fetchall()

# 取乙個結果

# cus.fetchone()

# 取10行資料

# cus.fetchmany(size=10)

# 關閉游標

# cus.close()

# cus.executemany()

try:

cus.execute(sql)

result = cus.fetchone()

print(result)

except exception as e:

raise e

finally:

cus.close()

conn.close()

二、建立表,常用命令。

1.建立表命令如下。

案例三create table 表名(

列名 資料型別 not null

create table stdunet(

stdid int not null,

stdname varchar(100),

age int,

*** enum('m', 'f'),

score int);

'123' varchar(10)

'123 ' char(10)

課堂糾錯案例四、

grant all privileges on *.* to 'user1'@'%' identified by '123456' with grant option;

你這個user1使用者,只能對所有的庫,所有的表進行增刪改查等,沒有對其他使用者進行授權 user2就沒法授權

2.常用命令。

1>.select 列名 from 表名 where 條件判斷

select * from sutdent where group by stdname

a, c where a.id = c.組id

select a.id a.name, c.id from a join c on c.組id = a.id and ^^^^

select * from table_name limiet 10;

show create table_name

desc table_name

2>.insert into table_name (id, name, age) values(1, 'ling', 18), (), (), ();

3>.delete from table where 條件判斷

4>truncate 只清楚資料,不刪除表結構

drop 表結果都給你乾掉了

5>.update table_name set 列名=***, where 條件

6>.create index 庫名_表名_列名1_列名2 (列名1, 列名2)

M0M1系列模組其它常用AT指令配置

m0m1系列模組除了一些基本的wifi功能指令,還增加了一些模組的配置指令,以適用於更多的應用場景。而這些指令是經過對客戶的應用需求不斷梳理而來的。這樣就可以使使用者在使用時有了更好的靈活性。本篇將對以下指令作詳細的分析。1.設定wifi模組在at指令下的回顯功能 at e 功能 開啟 關閉回顯功能...

M檔案與M函式

函式檔案由function語句引導,其基本結構為 function 輸出參數列 函式名 輸入參數列0 注釋說明部分 函式體語句 其中以function開頭的一行為引導行,表示該m檔案是乙個函式。函式名的命名規則與變數名相同。輸入形參為函式的輸入引數,輸出形參為函式的輸出型引數。當輸出從形參多於乙個時...

什麼是M2M模型以及M2M團隊計畫

什麼是 m2m模型 m2m模型是巨集觀到微觀 macro to micro 演算法模型的簡稱,但巨集觀到微觀只是對這個模型最為概括,最為簡潔的描述而已,它遠不能涵蓋這個模型的全部細節。m2m模型不僅僅強調巨集觀到微觀的解決問題的思路的重要性,更重要的是,它告訴人們如何從巨集觀到微觀去解決問題,巨集觀...