zoukankan      html  css  js  c++  java
  • Python MySQLdb insert(插入) 封装

    def insert_data(dbName,data_dict):
    
        try:
    
            data_values = "(" + "%s," * (len(data_dict)) + ")"
            data_values = data_values.replace(',)', ')')
    
            dbField = data_dict.keys()
            dataTuple = tuple(data_dict.values())
            dbField = str(tuple(dbField)).replace("'",'')
            conn = MySQLdb.connect(host="xxxxxxxxxx", user="xxxxxx", passwd="xxxxxx", db="xxxxxxx", charset="utf8")
            cursor = conn.cursor()
            sql = """ insert into %s %s values %s """ % (dbName,dbField,data_values)
            params = dataTuple
            cursor.execute(sql, params)
            conn.commit()
            cursor.close()
    
            print "=====  插入成功  ====="
            return 1
    
        except Exception as e:
            print "********  插入失败    ********"
            print e
            return 0

    使用说明:

      

    # 数据库的名称
    dbName = "oversea_thierrydemaigret_2017_2_zc"
    # 插入的数据
    data_dict = { "session_url": session_url, "session_address": session_address, "hashcode": hashcode, "create_time": create_time, "session_name": session_name, "item_auct_time": item_auct_time, }
    result11
    = insert_data(dbName, data_dict)
    print result11
  • 相关阅读:
    Nagios HTTP WARNING: HTTP/1.1 403 Forbidden
    nagios监控的安装
    Linux里使用rz和sz命令
    Mariadb安装
    ubuntu16.04下载地址
    安装.msi格式安装包
    英文语法检测工具
    正确引用R及R包
    West world 西部世界
    Altered Carbon 碳变/副本
  • 原文地址:https://www.cnblogs.com/xuchunlin/p/8309692.html
Copyright © 2011-2022 走看看