美文网首页
mysql数据库插入

mysql数据库插入

作者: 日落_3d9f | 来源:发表于2020-01-12 13:58 被阅读0次

    代码

    # -*- coding:utf-8 -*-
    
    import pymysql.cursors
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    # Connect to the database
    connection = pymysql.connect(host='localhost',
                                 user='root',
                                 password='',
                                 db='gongchang',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    
    try:
        with connection.cursor() as cursor:
            sql = "INSERT INTO `gc_spider` (`name`, `url`,`pid`) VALUES (%s, %s, %s)"
            cursor.execute(sql, ('苏州东菱振动试验仪器有限公司', '','xlTM-TogKuTw8WCD3fpSm0-xsvPsv9c3BQmd'))
        connection.commit()
        
        with connection.cursor() as cursor:
            sql = "SELECT count(name) as count FROM `gc_spider` WHERE `name`=%s"
            cursor.execute(sql, ('苏州东菱振动试验仪器有限公司',))
            result = cursor.fetchone()
            if(result['count']>0):
                print('have')
            else:
                print('no')
    
    finally:
        connection.close()
    

    相关文章

      网友评论

          本文标题:mysql数据库插入

          本文链接:https://www.haomeiwen.com/subject/jkgiactx.html