farcepest / MySQLdb1

MySQL database connector for Python (legacy version)
https://sourceforge.net/projects/mysql-python/
667 stars 318 forks source link

program uses time.sleep with the execute method working wrong #109

Closed drshiki closed 8 years ago

drshiki commented 8 years ago

it is my code for inserting data:

while True:
        item ={}
        tmp = DHT11.dht11_read()
        if  tmp is not None:
                item.update(tmp)
        else:
                item.update({'humidity' : 0})
        item.update(BH1750.bh1750_read())
        item.update(BMP180.bmp180_read())

        print "humidity = %d %% RH" %(item['humidity'])
        print "illuminance = %.2f lx" %(item['illuminance'])
        print "temperature = %.1f *C" %(item['temperature'])
        print "pressure = %.1f hPa" %(item['pressure'])
        print "altitude = %.2f m" %(item['altitude'])

        conn = MySQLdb.Connection(host="localhost", user="root", passwd="123456",\
        charset="UTF8")
        conn.select_db('mms')
        conn.autocommit(True);
        cursor = conn.cursor()
        cursor.execute("insert into t_data(humidity, temperature, illuminance, ppm, \
        altitude, pressure, sea_level_press, time) values(%s, %s, %s, %s, %s, %s, %s, null)", \
        (item['humidity'], item['temperature'], item['illuminance'], 0, item['altitude'], \
        item['pressure'], 0))
        #conn.commit();
        cursor.close()
        conn.close()
        time.sleep(1800)

it is the inserted items in my database:

    +---------+----------+-------------+-------------+------+----------+----------+-----------------+---------------------+
    | item_id | humidity | temperature | illuminance | ppm  | altitude | pressure | sea_level_press | time                |
    +---------+----------+-------------+-------------+------+----------+----------+-----------------+---------------------+
    |     259 |        0 |        24.9 |       20.00 | 0.00 |    30.93 |  1009.54 |            0.00 | 2016-03-19 09:04:25 |
    |     260 |       62 |        24.9 |       19.17 | 0.00 |  1009.54 |  1009.54 |            0.00 | 2016-03-19 09:04:58 |
    |     261 |       62 |        25.3 |        4.17 | 0.00 |    30.35 |  1009.52 |            0.00 | 2016-03-19 09:14:30 |
    |     262 |       62 |        25.5 |       10.83 | 0.00 |    31.10 |  1009.50 |            0.00 | 2016-03-19 09:22:45 |
    +---------+----------+-------------+-------------+------+----------+----------+-----------------+---------------------+

look at the last column, it is not 1800 seconds interval in tow records, and look at the _row 2 col 6_, it also inserted wrong, this col name is altitude however insert the value of col pressure, though it insert the right value most time. and the print statements always execute correctly with right value and 1800 seconds interval. I checked my code over last night but not found any, what is wrong?

ghost commented 8 years ago

@touchEngine What's the type of column time? From your code, column time assigned to null, which I don't think it would make sense. You probably should define column time like this: create table t_data(time datetime not null default current_timestamp);

drshiki commented 8 years ago

@lasteye my time col definition is

| time            | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |

I have solved this problem, I reboot my computer and run this code correctly, this issue was led by background process which runs the previous version my test code, I never know python interpreter can run the file which people are modifing, it is not the issue of MySQLdb but of my code, and thx for your reply