python連線mysql 查詢一條和多條

2021-10-14 14:37:04 字數 874 閱讀 4884

from pymysql import

*def

main()

:# 建立connection連線

conn = connect(host=

'localhost'

,port=

3306

,user=

'root'

,password=

'mysql'

,database=

'jing_dong'

,charset=

'utf8'

)# 獲得cursor物件

cs1 = conn.cursor(

)# 執行select語句,並返回受影響的行數:查詢一條資料

count = cs1.execute(

'select id,name from goods where id>=4'

)# 列印受影響的行數

print

("查詢到%d條資料:"

% count)

for i in

range

(count)

:# 獲取查詢的結果

result = cs1.fetchone(

)# 列印查詢的結果

print

(result)

# 查詢多條

# result = cs1.fetchall()

# print(result)

# 關閉cursor物件

cs1.close(

) conn.close(

)if __name__ ==

'__main__'

: main(

)

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢on MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...