zoukankan      html  css  js  c++  java
  • 操作数据库增加字段,判断数值增减,对号如坐

    首先呢,把数据导入到MySQL数据库进行查看,在加一个字段tbody。
    下面我给大家说一下下面代码的意思:
    首先python链接MySQL数据库
    import mysql.connector
    自己数据库的名字、端口号、密码、ip
    db = mysql.connector.connect(user='名',password='密码',host='127.0.0.1',port=端口,db='数据库名')
    cursor = db.cursor()
    下面是查询xlsx表格的内容,查询ownership表的所有内容,在python里显示出来
    cursor.execute("SELECT * FROM ownership")
    rows = cursor.fetchall()
    for循环 是循环的各个字段的内容在rows里
    for row in rows:
    e是ID号
    e=row[0]
    a是字段的内容
    a=row[4]
    b是字段的内容
    b=row[5]
    c是字段的内容
    c=row[6]

    下面的if判断是增持与减持,在判断对号入座,更新tbody字段
    if '-' in b:
    demo1 = a+'增持'+b+'股均价'+c+'元'
    sql = "update ownership SET tbody='"+demo1+"'WHERE id="+str(e)
    cursor.execute(sql)
    db.commit()

    else:
    demo2 = a+'减持'+b+'股均价'+c+'元'
    sql = "update ownership SET tbody='"+demo2+"'WHERE id="+str(e)
    cursor.execute(sql)
    db.commit()
    大家用的时候可以把‘-‘用re.replace或re.sub替换为空,在更新字段
  • 相关阅读:
    POJ2960 S-Nim
    HDU1850 Being a Good Boy in Spring Festival
    描述性统计-1
    基础-1
    .Net程序调试
    家装设计
    ACDSee技巧
    Timeline Maker 用法小结
    Windows 7 操作系统核心文件
    艺术字的操作
  • 原文地址:https://www.cnblogs.com/lianghongrui/p/7001221.html
Copyright © 2011-2022 走看看