MySQL之pymysql模組

來源:互聯網
上載者:User

標籤:使用者   串連資料庫   res   password   base   元組   body   csharp   from   

import pymysql#s連結資料庫conn = pymysql.connect(    host = ‘127.0.0.1‘,  #被串連資料庫的ip地址    port = 3306,         #資料庫服務端連接埠號碼    user = ‘root‘,       #使用者名稱    password = ‘123456‘, #密碼    database = ‘db1‘,    #選擇庫    charset = ‘utf8‘     #編碼格式)#拿到執行sql語句的遊標cur = conn.cursor()#查詢語句select_sql = ‘select * from auth‘#執行sql語句cur.execute(select_sql)#擷取單條查詢結果# res1 = cur.fetchone()#擷取指定行數的查詢結果# res2 = cur.fetchmany(3)#擷取全部查詢結果res3 = cur.fetchall()#輸出查詢結果#注意,如果有多條擷取查詢的語句,遊標會從上一次的位置開始查詢#比如說如果有兩條 cur.fetchall()   那麼第二次查詢結果為空白print(res3)#插入語句#可以直接寫插入的值,也可以用%s來佔位,然後傳參insert_sql = ‘insert into auth (name,age,address) values (%s,%s,%s)‘#傳入參數的時候用元組或者列表來裝插入值cur.execute(insert_sql,(‘auth1‘,18,‘北京‘))#提交資料conn.commit()#關閉遊標cur.close()#關閉連結conn.close()

  

MySQL之pymysql模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.