Insert Data to MySQL From ADS1115 Python
<p>i have a problem extracting data from accelerometer sensor in raspberry pi using ADC ADS1115.</p>

<p>I'm using this code for inserting the data to mysql</p>

<pre><code># Author: Tony DiCola
# License: Public Domain
# Import the ADS1x15 module.
import Adafruit_ADS1x15
import MySQLdb
import time
import datetime
# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()
# Or create an ADS1015 ADC (12-bit) instance.
#adc = Adafruit_ADS1x15.ADS1015()

# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
#adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1

time_sensor = time.time()
# Main loop.
x = [1]*4
y = [2]*4
z = [3]*4   
for i in range(4):
    x[i] = adc.start_adc(i, gain=GAIN)
    y[i] = adc.start_adc(i, gain=GAIN)
    z[i] = adc.start_adc(i, gain=GAIN)
# Read the specified ADC channel using the previously set gain value.
# Once continuous ADC conversions are started you can call get_last_result() to
db = MySQLdb.connect("localhost", "root", "raspberry", "sensor_log")
curs=db.cursor()

while True:
    try:
        curs.execute("""INSERT INTO table_sensor_log(time, koordinatx, koordinaty, koordinatz) 
        values(%s,%s,%s,%s)""",(time_sensor,x[i],y[i],z[i]))
        db.commit()
    except:
        print "Error"
        db.rollback()
    time.sleep(1)
db.close()
</code></pre>

<p>The problem is when I run that script, the received data was duplicated, that script only getting data from the first data of accelerometer sensor and then insert it repeatedly.</p>

<p>This is what i get.</p>

<pre><code>+-------+------------+------------+------------+------------+
| id    | time       | koordinatX | koordinatY | koordinatZ |
+-------+------------+------------+------------+------------+
| 24743 | 1472624612 |      15443 |      20351 |      20454 |
| 24744 | 1472624612 |      15443 |      20351 |      20454 |
| 24745 | 1472624612 |      15443 |      20351 |      20454 |
| 24746 | 1472624612 |      15443 |      20351 |      20454 |
| 24747 | 1472624612 |      15443 |      20351 |      20454 |
| 24748 | 1472624612 |      15443 |      20351 |      20454 |
+-------+------------+------------+------------+------------+
</code></pre>

<p>I need the real data from the sensor, if i use print, the data will correctly displaying, but when i inserting it to mysql, the data will be like that.</p>

<p>Move cursor initialization inside loop</p>

<pre><code>while True:
    try:
        curs=db.cursor()
        curs.execute("""INSERT INTO table_sensor_log(time, koordinatx, koordinaty, koordinatz) 
        values(%s,%s,%s,%s)""",(time_sensor,x[i],y[i],z[i]))
        db.commit()
        curs.close()
    except:
        print "Error"
        db.rollback()
    time.sleep(1)
</code></pre>

php python mysql raspberry-pi2 adc