python資料庫操作pymysql

來源:互聯網
上載者:User

標籤:

安裝資料庫:

pip3 install pymysql

進行資料庫的更新、插入、查詢等操作:

 1 #!/usr/bin/python3.4 2 # -*- coding: utf-8 -*- 3  4 #-----------------原表格----------------- 5  6 #+-------+-----------+------------+------+ 7 #| mid   | name      | birth      | sex  | 8 #+-------+-----------+------------+------+ 9 #| G0001 | 杜意意    | 1975-04-18 | 0    |10 #| G0002 | 李玉      | NULL       | 1    |11 #| H0001 | 李加      | NULL       | 0    |12 #| N0001 | 小小      | 1980-11-23 | 1    |13 #+-------+-----------+------------+------+14 15 import pymysql16 17 # 串連資料庫18 mysql = pymysql.connect(host="localhost", user="root", passwd="1111", db="test", charset="utf8")19 20 # 擷取操作遊標21 cur = mysql.cursor()22 23 # 尋找24 lookup = input(‘請輸入尋找語句:‘)25 # 將尋找語句放入操作中26 # 執行成功後sta值為127 sta = cur.execute(lookup)28 # 列印出尋找的東西29 # 這裡也可以編碼item[].decode(‘UTF-8‘)30 for item in cur:31     print("Id=" + str(item[0]) + " name=" + str(item[1]) + " birth=" + str(item[2]) + " sex=" + str(item[3]))32 33 # 插入、更新34 # 插入王五35 # insert into customer(mid,name,birth,sex) values(‘G0001‘,‘王五‘,‘1992-01-03‘,‘1‘);36 # 將G0002名字改為李玉枝,生日補齊37 # update customer set name=‘李玉枝‘,birth=‘1980-09-09‘ where mid=‘G0002‘;38 insert = input(‘請輸入插入(更新)語句:‘)39 # 將尋找語句放入操作中40 # 執行成功後sta值為141 sta = cur.execute(insert)42 # 最後確定後下面語句將真正插入進去43 # 如果只是測試代碼對不對可以將其注釋掉44 mysql.commit()45 46 # 關閉操作遊標47 cur.close()48 49 # 關閉資料庫50 mysql.close()

 

python資料庫操作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.