如何用python-pymysql更新MySQL表中任意字段为长尾词?

2026-04-19 21:501阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1067个文字,预计阅读时间需要5分钟。

如何用python-pymysql更新MySQL表中任意字段为长尾词?

目录更新MySQL任意字段中的数据使用Python操作MySQL数据库:PyMySQL或mysql-connector使用mysql-connector连接数据库使用mysql-connector连接数据库对MySQL数据库进行操作总结更新MySQL任意字段中的数据

目录
  • 更新MySQL随意字段里的数据
  • python操作mysql数据库:使用PyMySQL或mysql-connector
    • 使用mysql-connector连接数据库
    • 使用mysql-connector连接数据库
    • 对mysql数据库进行操作
  • 总结

    更新MySQL随意字段里的数据

    下面是我的mysql所有字段名

    若字段名太多不建议使用以下方法

    这里sql语句要注意一下双引号里面用单引号.

    def Changehous(): """ 修改mysql里的任意字段数据 """ host = "localhost"#默认为localhost user = "root"#用户名 passwd = "000000"#此处输入连接mysql的密码 port = "3306"#端口号可以不输入 database = "hous"#需要连接的数据库名(不是表名) db = pymysql.connect(host, user, passwd, database)#连接mysql cursor = db.cursor()#创建游标 #定义一个列表,来来装自己的字段名 #若字段名过多的话不建议使用这种方法 title = ['residential','house','area','orientation','floor','years','totalprice'] field = str(input("请输入要修改的字段名:"))#控制台输入 if field in title: #如果输入的字段名在title里面,那么就可以进行查询了. name = str(input("请输入要修改的内容")) id = int(input("请修改要更新内容的id:")) if field == title[0]: sql = "update mashine set residential='{}' where id='{}'".format(name,id) elif field == title[1]: sql = "update mashine set house='{}' where id='{}'".format(name, id) elif field == title[2]: sql = "update mashine set area='{}' where id='{}'".format(name, id) elif field == title[3]: sql = "update mashine set orientation='{}' where id='{}'".format(name, id) elif field == title[4]: sql = "update mashine set floor='{}' where id='{}'".format(name, id) elif field == title[5]: sql = "update mashine set years='{}' where id='{}'".format(name, id) elif field == title[6]: sql = "update mashine set totalprice='{}' where id='{}'".format(name, id) else: print("输入有误,没有查询到该字段!") try: cursor.execute(sql) db.commit()#提交给数据库 print("修改成功") except Exception as e: print("修改失败") finally: db.close()#关闭数据库 cursor.close()#关闭游标

    这里可以看到我的表中数据

    现在我们运行程序来修改某一字段

    想改那个字段就输入那个字段名,这里用我的residential字段做个示范

    原内容为:大道1号,现在我将它修改为:小道二号,我的主键id为1205

    以上就是更新任意字段数据的代码,有需要的可以复制

    python操作mysql数据库:使用PyMySQL或mysql-connector

    使用mysql-connector连接数据库

    1、先安装mysql-connector

    pip install mysql-connector

    2、连接mysql数据库

    1、先安装mysql-connector

    pip install PyMySQL

    2、连接mysql数据库

    对mysql数据库进行操作

    1、获取操作游标

    2、执行SQL语句,这里举例查看数据库版本,来验证数据库是否连接成功

    运行程序结果:

    3、创建数据库“XJ_test”

    4、查询当前有哪些数据库

    5、删除数据库“XJ_test2”

    6、在“XJ_test”数据库下创建数据表

    如何用python-pymysql更新MySQL表中任意字段为长尾词?

    7、主键设置:给xj_table表添加id列,并将该字段设置为主键,主键起始值为1,逐步递增

    如果还没有创建表,可以在创建表的时候直接给字段设置主键:

    8、删除id字段

    9、插入数据

    10、批量插入数据

    11、插入记录后返回该主键id(如果该数据表具有id自增主键列)

    12、查询数据

    (1)fetchall():查询所有记录

    (2)fetchmany(n):查询n个记录

    (3)fetchone():查询1条数据

    13、查询指定字段数据

    14、删除数据

    15、修改表数据

    16、删除表

    17、删除数据库

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

    本文共计1067个文字,预计阅读时间需要5分钟。

    如何用python-pymysql更新MySQL表中任意字段为长尾词?

    目录更新MySQL任意字段中的数据使用Python操作MySQL数据库:PyMySQL或mysql-connector使用mysql-connector连接数据库使用mysql-connector连接数据库对MySQL数据库进行操作总结更新MySQL任意字段中的数据

    目录
    • 更新MySQL随意字段里的数据
    • python操作mysql数据库:使用PyMySQL或mysql-connector
      • 使用mysql-connector连接数据库
      • 使用mysql-connector连接数据库
      • 对mysql数据库进行操作
    • 总结

      更新MySQL随意字段里的数据

      下面是我的mysql所有字段名

      若字段名太多不建议使用以下方法

      这里sql语句要注意一下双引号里面用单引号.

      def Changehous(): """ 修改mysql里的任意字段数据 """ host = "localhost"#默认为localhost user = "root"#用户名 passwd = "000000"#此处输入连接mysql的密码 port = "3306"#端口号可以不输入 database = "hous"#需要连接的数据库名(不是表名) db = pymysql.connect(host, user, passwd, database)#连接mysql cursor = db.cursor()#创建游标 #定义一个列表,来来装自己的字段名 #若字段名过多的话不建议使用这种方法 title = ['residential','house','area','orientation','floor','years','totalprice'] field = str(input("请输入要修改的字段名:"))#控制台输入 if field in title: #如果输入的字段名在title里面,那么就可以进行查询了. name = str(input("请输入要修改的内容")) id = int(input("请修改要更新内容的id:")) if field == title[0]: sql = "update mashine set residential='{}' where id='{}'".format(name,id) elif field == title[1]: sql = "update mashine set house='{}' where id='{}'".format(name, id) elif field == title[2]: sql = "update mashine set area='{}' where id='{}'".format(name, id) elif field == title[3]: sql = "update mashine set orientation='{}' where id='{}'".format(name, id) elif field == title[4]: sql = "update mashine set floor='{}' where id='{}'".format(name, id) elif field == title[5]: sql = "update mashine set years='{}' where id='{}'".format(name, id) elif field == title[6]: sql = "update mashine set totalprice='{}' where id='{}'".format(name, id) else: print("输入有误,没有查询到该字段!") try: cursor.execute(sql) db.commit()#提交给数据库 print("修改成功") except Exception as e: print("修改失败") finally: db.close()#关闭数据库 cursor.close()#关闭游标

      这里可以看到我的表中数据

      现在我们运行程序来修改某一字段

      想改那个字段就输入那个字段名,这里用我的residential字段做个示范

      原内容为:大道1号,现在我将它修改为:小道二号,我的主键id为1205

      以上就是更新任意字段数据的代码,有需要的可以复制

      python操作mysql数据库:使用PyMySQL或mysql-connector

      使用mysql-connector连接数据库

      1、先安装mysql-connector

      pip install mysql-connector

      2、连接mysql数据库

      1、先安装mysql-connector

      pip install PyMySQL

      2、连接mysql数据库

      对mysql数据库进行操作

      1、获取操作游标

      2、执行SQL语句,这里举例查看数据库版本,来验证数据库是否连接成功

      运行程序结果:

      3、创建数据库“XJ_test”

      4、查询当前有哪些数据库

      5、删除数据库“XJ_test2”

      6、在“XJ_test”数据库下创建数据表

      如何用python-pymysql更新MySQL表中任意字段为长尾词?

      7、主键设置:给xj_table表添加id列,并将该字段设置为主键,主键起始值为1,逐步递增

      如果还没有创建表,可以在创建表的时候直接给字段设置主键:

      8、删除id字段

      9、插入数据

      10、批量插入数据

      11、插入记录后返回该主键id(如果该数据表具有id自增主键列)

      12、查询数据

      (1)fetchall():查询所有记录

      (2)fetchmany(n):查询n个记录

      (3)fetchone():查询1条数据

      13、查询指定字段数据

      14、删除数据

      15、修改表数据

      16、删除表

      17、删除数据库

      总结

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。