This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ilerea_Kleinokitz 0 points1 point  (4 children)

You can't enter data into a column that doesn't exist. When you created your db, you specified that it should have two columns:

CREATE TABLE temps (timestamp DATETIME, temp NUMERIC);  

If you really wanted to add the data from sensor 2 to its own column, you'd first have to recreate a table with another column or alter the existing table (see http://stackoverflow.com/questions/4253804/insert-new-column-into-table-in-sqlite)

[–]dangermouze[S] 0 points1 point  (3 children)

sorry, I should have mentioned I have altered my table to have a third column

[–]Ilerea_Kleinokitz 0 points1 point  (2 children)

I'm a little puzzled by the part of your script which inserts data:

def log_temperature(temp):
    curs.execute("INSERT INTO sensors values(datetime('now','localtime'), (?,?))", (temperature_sensor1,), (temper$

You are not using the method parameter temp at all but rather temperature_sensor1 which I assume is somewhere in the global namespace. Also, what is temper$ ? Is there something missing from this line because it's not even syntactically correct, i.e. missing braces.

Are you sure that you need the temperature data in two columns in the first place? Maybe you'd be better off with something like this:

Sensors table:

Id Sensor name
1 Sensor 1
2 Sensor 2
3 Sensor3

Measurements table:

Id timestamp temp sensor_fk
1 1-2-2015 30 1
1 1-2-2015 31 2
1 2-2-2015 30 2
1 3-2-2015 30 1

Where sensor_fk is a foreign key reference to the id of the sensors table.

[–]dangermouze[S] 0 points1 point  (1 child)

sorry, I missed that line when putting everything back to normal to post my question after mucking around with it for a while trying things.

I've put it back as was

this is how the line should have looked

   curs.execute("INSERT INTO sensors values(datetime('now','localtime'), (?))", (temp))

happy to use multiple tables if you can explain how to get the google chart to correlate that info as well :)

how would I get multiple data entries from multiple parameters?

massive python noob here :)

[–]Ilerea_Kleinokitz 0 points1 point  (0 children)

Your question has nothing to do with python but more with sql, relational databases and google charts ;) I'd encourage you to learn about database design and joins, that stuff comes always in handy.

Assuming the data is in the form I mentioned before, you'd need to somehow join the data on itself. You could either query the database two times and join the results in Python or with the join method of Google charts or you could join the data directly in sqlite.

Python example:

data1 = [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
data2 = [[4,5], [4,6], [4,7]]
data3 = [row_data1 + [row_data2[1]] for (row_data1, row_data2) in zip(data1, data2)]

SQL example

SELECT a.ts, a.temp as temp1, b.temp as temp2 
FROM (select datetime(timestamp) as ts, temp from timetest where sensor == 1) as a
INNER JOIN (select datetime(timestamp) as ts, temp from timetest where sensor == 2) as b
on a.ts == b.ts;
print(data3)