Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5050

Beginners • Re: mariadb: insert into error: thinks a variable in the values list is a column

$
0
0
I am trying to insert data into a mariadb table (data) via a python script using the following command:

cur.execute(" INSERT INTO data( name, value, mod_date) VALUES( 'temperature', temp_value, current_time )")

I get the following error message: Error writing data: Unknown column 'temp_value' in 'field list'

It does recognize current_time as a value.

If I change temp_value to a string, like '32', the INSERT works.
Also, if I change 'temperature' to a variable, temperature, it thinks its a column name.

Below is a description of the table, data:

Code:

MariaDB [sensor1]> describe data;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| id       | int(11)     | NO   | PRI | NULL    | auto_increment || name     | varchar(50) | NO   |     | NULL    |                || value    | varchar(50) | YES  |     | NULL    |                || mod_date | date        | YES  |     | NULL    |                |+----------+-------------+------+-----+---------+----------------+
Much appreciation from this Newbe!
The "value" column is defined as VARCHAR, which is to say, a string. You attempted to insert a numerical value. Note that when you inserted a string value, it did work. You need to either recast your temperature value as a string, or change your table to hold numeric data in "value".

If I were writing this, I would include a placeholder value for the "id" column, probably zero.
Example...

Code:

create table fee_master (    fm_rowid           int not null primary key auto_increment,    fm_conid           varchar(6) not null,    fm_year            smallint not null,    fm_schedule        smallint,    fm_desc            varchar(32),    fm_rate            smallint,    fm_rate_real       smallint,    unique index fm_real_key (fm_conid, fm_year, fm_schedule));insert into fee_mastervalues(0, "DDC", 2025, 1, "Full con", 8000, 7000),(0, "DDC", 2025, 2, "One day", 5000, 5000),(0, "DDC", 2025, 3, "Upgrade", 3000, 3000),(0, "DDC", 2025, 4, "Badge Replacement", 500, 500),(0, "DDC", 2025, 5, "Discounted Admission", 7000, 7000),(0, "DDC", 2025, 6, "In Tow Badge", 500, 500);

Statistics: Posted by W. H. Heydt — Fri Sep 20, 2024 8:22 pm



Viewing all articles
Browse latest Browse all 5050

Trending Articles