Demo entry 6719548

py

   

Submitted by anonymous on Mar 12, 2018 at 05:25
Language: Python 3. Code size: 1.5 kB.

import pymysql

conn = pymysql.connect(
    host='localhost',
    db='studentscore',
    user='root',
    passwd='123456',
    charset='utf8',
    use_unicode=True
)
cursor = conn.cursor()
sql = 'SELECT id,name,price FROM amapcommercialhouseitem WHERE price IS NOT NULL GROUP BY name'
cursor.execute(sql)
Groups = cursor.fetchall()  # Groups是从"amapcommercialhouseitem"读取到的信息
cursor.close()
conn.close()

conn2 = pymysql.connect(
    host='localhost',
    db='studentscore',
    user='root',
    passwd='123456',
    charset='utf8',
    use_unicode=True
)
conn3 = pymysql.connect(
    host='localhost',
    db='studentscore',
    user='root',
    passwd='123456',
    charset='utf8',
    use_unicode=True
)
for temp in Groups:
    print('id:', temp[0], '  name:', temp[1], '  price:', temp[2])
    # conn2.ping(True)  # Check if the server is alive
    cursor2 = conn2.cursor()
    FillGroup = "SELECT id,name,price FROM amapcommercialhouseitem WHERE price IS NULL AND name = %s"
    cursor2.execute(FillGroup, (temp[1]))  # temp[1]是name(小区名)
    GroupMembers = cursor2.fetchall()
    cursor2.close()
    for g in GroupMembers:
        # conn3.ping(True)  # Check if the server is alive
        cursor3 = conn3.cursor()
        sql = """
                        UPDATE amapcommercialhouseitem SET price=%s WHERE id=%s
                """
        cursor3.execute(sql, (temp[2], g[0]))
        conn3.commit()  # 提交,不然无法保存新建或者修改的数据
        cursor3.close()

conn2.close()
conn3.close()

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).