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() 操作

  • 相关阅读:
    页面可视化搭建工具前生今世
    16、Redis手动创建集群
    15、Redis的集群
    14、Redis的复制
    13、Redis的发布订阅模式
    12、Redis的事务
    11、Redis的持久化(RDB、AOF)
    9、Redis处理过期keys的机制
    8、Redis中sort命令详解
    7、Redis中对ZSet类型的操作命令
  • 原文地址:https://www.cnblogs.com/plefan/p/13924332.html
Copyright © 2011-2022 走看看