zoukankan      html  css  js  c++  java
  • python 操作mysql

    import mysql.connector 
    import xlrd
    mydb=mysql.connector.connect(host="125.65.*.*",user="pjump",passwd="******",database="pjump")
    mycursor=mydb.cursor()
    #mycursor.execute("create table pytest (id VARCHAR(255),domian VARCHAR(255))")
    #mycursor.execute("show tables")
    #mycursor.execute("drop table pytest")
    workbook=xlrd.open_workbook(r"e:domain.xls")
    table=workbook.sheet_by_name('Sheet1')
    domainlist=[]
    s=()
    for m in range(table.nrows):
        if m<table.nrows and m>0:
            s=(int(table.cell(m,0).value),table.cell(m,1).value)
            domainlist.append(s)
    #print (domainlist)        
    
    # #@@@@@@@@@@@@@@插入@@@@@@@@@@
    # sql= "insert into pytest (id,domian) values (%s,%s)"
    # mycursor.executemany(sql,domainlist)
    # mydb.commit()
    # #@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    # #@@@@@@@@@@@@执行查询@@@@@@@@
    # sql='select *from pytest'
    # mycursor.execute(sql)
    # result= mycursor.fetchall()
    # #fetchone 查询结果第一条,fetchall  所有查询结果
    # for x in result:
    #     print (x)
    # #@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    #@@@@@@@@@@@@@@@@@@@删除@@@@@@@@@
    #sql='dorp table if exists pytest'
    # mycursor.execute(sql)
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    
    
    #@@@@@@@@@@@@@@@@@@@删除@@@@@@@@@
    #sql='update pytest set domain = %s where domain = %s'
    #val = ("baidu.com","qq.com")
    #mycursor.execute(sql,val)
    #mydb.commit()
    #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

    修改更新操作时注意mydb.commit() 操作

  • 相关阅读:
    Ubuntu 18.04 设置静态IP
    面试问题总结
    hadoop集群搭建流程
    胡适:天下没有白费的努力
    学习的技术内容
    Windows MySql安装
    MySql 基础知识
    windows maven 安装与配置
    Logstash抽取数据到Elasticsearch(1)
    后台运行导入数据库
  • 原文地址:https://www.cnblogs.com/plefan/p/13924332.html
Copyright © 2011-2022 走看看