mysql+python缺失数据库完善小脚本

来源:互联网 发布:人人店分销源码 编辑:程序博客网 时间:2024/05/26 02:52

先贴上一下,这两天写出来的小脚本,太波折啦,累死啦(有空再更新细节,)

需求:

有一个数据库你需要每天存在20家公司的信息,但是每家公司网站获取信息是有难度的,可能一天中你只能获取19或者18家公司的信息,但是你又不想今天的信息不完整,于是你想到一个好办法,今天没获取到信息的公司就用前一天的信息填充(前一天的肯定是完整的)。

设计思路

  • 判断当天是否缺少公司信息

  • 缺少的条件下,根据缺少的公司名字提取前一天的信息

  • 将提取的公司信息存入数据库(注意修改日期和时间之类的信息,更新为当天的)
    我的代码:

# -*- coding:utf-8 -*-import MySQLdbimport jsonfrom company import companysimport sysreload(sys)sys.setdefaultencoding('utf8')#检测是否缺少数据def check(id):    sql = "select * from testtable2 where id = " + str(id)    cursor.execute(sql)    rs = cursor.fetchall()    return len(rs)#获取当天的信息,主要是为了插入数据时需要修改uptimedef uptime(id):    sql = "select * from testtable2 where id = " + str(id)    cursor.execute(sql)    rs = cursor.fetchone()    return rs#获取缺少的公司名字列表def lack(id):    sql = "select * from testtable2 where id = " + str(id)    cursor.execute(sql)    rs = cursor.fetchall()    if len(rs) == 21:        temp = "无缺少数据"        return temp    else:        temp = []        res = []        sql_1 = "SELECT name FROM testtable2 WHERE id = " + str(id)        cursor.execute(sql_1)        rs_1 = cursor.fetchall()        for row in rs_1:            temp.append(row[0])        for row in companys:            if row not in temp:                res.append(row)        return res#获取一个公司的完整信息def get_lack_data(id, name):    name = name.decode('utf-8')    sql ="SELECT * FROM testtable2 WHERE name = "+ "'"+name + "'" + " and id = " +  "'"+ id + "'"    cursor.execute(sql)    rs = cursor.fetchone()    if cursor.rowcount>=1:        return rs    else:        return "请将时间再提前一天"#插入数据,这个需要注意两点:(1)插入信息的id要更新为当天 (2)插入信息的uptime需要更新为当天的def index_data(id,data):    res = []    for row in data:        res.append(row)    #更新id    temp = uptime(id)    res[5] = temp[5]    res[15] = id    sql = "INSERT INTO `testtable2` (`Price_slide`, `name`, `Recognition_rate`, `nature`, `Introduction`, `uptime`, `Price_Chinese`, `Call_num`, `Price_letter`, `link`, `Company_name`, `Company_address`, `Price_Calculation`, `Price_Click`, `Price_QA`, `id`, `QQ_num`, `Price_Click_1w`, `Price_letter_1w`, `Price_Chinese_1w`, `Website_record`, `Price_QA_1w`) VALUES" + "(" +"'"+res[0]+"'"+","+"'"+res[1]+"'"+","+"'"+res[2]+"'"+","+"'"+res[3]+"'"+","+"'"+res[4]+"'"+","+"'"+res[5]+"'"+","+"'"+res[6]+"'"+","+"'"+res[7]+"'"+","+"'"+res[8]+"'"+","+"'"+res[9]+"'"+","+"'"+res[10]+"'"+","+"'"+res[11]+"'"+","+"'"+res[12]+"'"+","+"'"+res[13]+"'"+","+"'"+res[14]+"'"+","+"'"+res[15]+"'"+","+"'"+res[16]+"'"+","+"'"+res[17]+"'"+","+"'"+res[18]+"'"+","+"'"+res[19]+"'"+","+"'"+res[20]+"'"+","+"'"+res[21]+"'" +")"    cursor.execute(sql)    conn.commit()    return "插入成功"conn = MySQLdb.Connect(host = '127.0.0.1',port = 3306,  user = 'root',passwd = 'root',db = 'test',charset = 'utf8')cursor = conn.cursor()id = '20170808'old_id = '20170807'if check(id)== 21:    print "信息完整!!!!"else:    res = lack(id)    for row in res:        print row        data = get_lack_data(old_id, row)        print (index_data(id,data))print check(id)conn.close()

遇到的问题

最多的问题就是编码问题,心累呀,第一处出现在根据缺少的公司信息获取前天公司的完整信息,mysql数据库里面是编码是utf-8,但是获取缺少公司的名字缺少unicode需要使用decode(‘utf-8’)进行转换一下(关于编码问题之前学了一点,如今应用到实际还是有点犯迷糊),之后就是更新数据这一项其实和前面的获取类似,刚开始因为里面有22列试了一下网上的方法:

#插入一条数据sqli="insert into student values(%s,%s,%s,%s)"cur.execute(sqli,('3','Huhu','2 year 1 class','7'))

结果一直显示mysql语法错误,后来才原来用%s转化为字符类型啦,编码不并没有改变,最后算是又采用了前面的比较复杂的方法,我一直觉得还是有简单的方法的,只是我能力有限。

原创粉丝点击