濮阳杆衣贸易有限公司

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

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

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

Python連接MySQL,進行數(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() 
 

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

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

您可能感興趣的文章:
  • 使用Python實現(xiàn)將多表分批次從數(shù)據(jù)庫導出到Excel
  • python如何解析復雜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ù)導出生成csv格式文件的方法
  • Python實現(xiàn)mysql數(shù)據(jù)庫更新表數(shù)據(jù)接口的功能
  • Python實現(xiàn)將sqlite數(shù)據(jù)庫導出轉(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é)構同步

標簽:東莞 南昌 吉安 合肥 武漢 潛江 長沙 江門

巨人網(wǎng)絡通訊聲明:本文標題《Python MySQL進行數(shù)據(jù)庫表變更和查詢》,本文關鍵詞  Python,MySQL,進行,數(shù)據(jù)庫,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Python MySQL進行數(shù)據(jù)庫表變更和查詢》相關的同類信息!
  • 本頁收集關于Python MySQL進行數(shù)據(jù)庫表變更和查詢的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    榕江县| 宜宾市| 临泉县| 屏边| 拜泉县| 高碑店市| 海兴县| 北安市| 古交市| 兰考县| 金坛市| 临武县| 聊城市| 泰顺县| 淳化县| 平陆县| 嘉善县| 理塘县| 宜兰市| 凤凰县| 义乌市| 任丘市| 安乡县| 监利县| 广河县| 华蓥市| 岗巴县| 黄平县| 金门县| 苏尼特右旗| 准格尔旗| 淮滨县| 黄陵县| 合作市| 邛崃市| 山阳县| 安龙县| 廊坊市| 平邑县| 苏尼特右旗| 萨嘎县|