濮阳杆衣贸易有限公司

主頁 > 知識庫 > Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢

Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢

熱門標(biāo)簽:昆明智能外呼系統(tǒng)中心 長安區(qū)違法建房地圖標(biāo)注 地圖標(biāo)注培訓(xùn) 電銷機(jī)器人公眾號推送 安國在哪里辦理400電話 手機(jī)用地圖標(biāo)注工具 南宋地圖標(biāo)注黃河華山 智能電銷機(jī)器人靠譜么 電銷機(jī)器人說明書

Python連接MySQL,進(jìn)行數(shù)據(jù)庫表變更和查詢:

python mysql insert delete query:

#!/usr/bin/python 
 
import MySQLdb 
def doInsert(cursor,db): 
  #insert 
  # Prepare SQL query to INSERT a record into the database. 
  sql = "UPDATE EMPLOYEE SET AGE = AGE+1 WHERE SEX = '%c'" %('M') 
  try: 
    cursor.execute(sql) 
    db.commit() 
  except: 
    db.rollback() 
 
def do_query(cursor,db): 
  sql = "SELECT * FROM EMPLOYEE \ 
     WHERE INCOME > '%d'" % (1000) 
  try: 
    # Execute the SQL command 
    cursor.execute(sql) 
    # Fetch all the rows in a list of lists. 
    results = cursor.fetchall() 
    print 'resuts',cursor.rowcount 
    for row in results: 
      fname = row[0] 
      lname = row[1] 
      age = row[2] 
      sex = row[3] 
      income = row[4] 
      # Now print fetched result 
      print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ 
          (fname, lname, age, sex, income ) 
  except: 
    print "Error: unable to fecth data" 
 
def do_delete(cursor,db): 
  sql = 'DELETE FROM EMPLOYEE WHERE AGE > {}'.format(20) 
  try: 
    cursor.execute(sql) 
    db.commit() 
  except: 
    db.rollback() 
 
def do_insert(cursor,db,firstname,lastname,age,sex,income): 
  sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ 
    LAST_NAME, AGE, SEX, INCOME) \ 
    VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ 
    (firstname,lastname,age,sex,income) 
  try: 
    cursor.execute(sql) 
    db.commit() 
  except: 
    db.rollback() 
  
# Open database connection 
# change this to your mysql account 
#connect(server,username,password,db_name) 
db = MySQLdb.connect("localhost","root","root","pydb" ) 
# prepare a cursor object using cursor() method 
cursor = db.cursor() 
do_query(cursor,db) 
doInsert(cursor,db) 
do_query(cursor,db) 
do_delete(cursor,db) 
do_query(cursor,db) 
do_insert(cursor,db,'hunter','xue',22,'M',2000) 
do_insert(cursor,db,'mary','yang',22,'f',5555) 
do_insert(cursor,db,'zhang','xue',32,'M',5000) 
do_insert(cursor,db,'hunter','xue',22,'M',333) 
do_query(cursor,db) 
# disconnect from server 
db.close() 
 

之后可以在此基礎(chǔ)上根據(jù)需要進(jìn)行封裝。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • 使用Python實現(xiàn)將多表分批次從數(shù)據(jù)庫導(dǎo)出到Excel
  • python如何解析復(fù)雜sql,實現(xiàn)數(shù)據(jù)庫和表的提取的實例剖析
  • python的mysql數(shù)據(jù)庫建立表與插入數(shù)據(jù)操作示例
  • python 獲取sqlite3數(shù)據(jù)庫的表名和表字段名的實例
  • Python獲取數(shù)據(jù)庫數(shù)據(jù)并保存在excel表格中的方法
  • Python實現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
  • Python實現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口的功能
  • Python實現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法
  • Python如何讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)
  • python數(shù)據(jù)庫操作常用功能使用詳解(創(chuàng)建表/插入數(shù)據(jù)/獲取數(shù)據(jù))
  • Python 如何實現(xiàn)數(shù)據(jù)庫表結(jié)構(gòu)同步

標(biāo)簽:東莞 南昌 吉安 合肥 武漢 潛江 長沙 江門

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢》,本文關(guān)鍵詞  Python,MySQL,進(jìn)行,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python MySQL進(jìn)行數(shù)據(jù)庫表變更和查詢的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    额济纳旗| 陇西县| 奉新县| 福海县| 白水县| 德阳市| 绍兴市| 读书| 宣化县| 新宾| 巴青县| 淮阳县| 长春市| 澄江县| 扶余县| 双柏县| 革吉县| 江阴市| 颍上县| 元阳县| 三河市| 乌拉特后旗| 陆良县| 德江县| 龙山县| 南昌县| 大石桥市| 嵊州市| 正定县| 敦煌市| 永春县| 晋城| 宜州市| 淄博市| 额济纳旗| 邮箱| 郯城县| 体育| 大港区| 池州市| 芦溪县|