Let's assmue I have the following table in a database called data.db:
CREATE TABLE species ( species_id int PRIMARY KEY, -- NCBI taxonomic code abbrev character(2), latin character varying(50), common character varying(20) );
How can I manually add new data into this database from python using sql?
The input function should look something like this:
Please define a 'species_id', that should be inserted to the database: Please define a 'latin', that should be inserted to the database: Please define a 'abbrev' that should be inserted to the database:
The code I have tried looks like this:
def data_add(conn, cursor, species_id, latin, abbrev): sql = "INSERT INTO species VALUE ('{species_id}', '{latin}', '{abbrev}', NULL);" A=input("Please define a 'species_id', that should be inserted to the database (used in task 5):") B=input("Please define a 'latin', that should be inserted to the database (used in task 5):") C=input("Please define a 'abbrev', that should be inserted to the database (used in task 5):") cursor.execute(sql,[A], [B], [C]) return cursor.fetchall()
EDIT - updated based on feedback
def data_add(conn, cursor, species_id, latin, abbrev): species_id = input("Please define a 'species_id':") latin = input("Please define a 'latin':") abbrev = input("Please define a 'abbrev':") sql = "insert into species(species_id, latin, abbrev) value (?, ?, ?);" cursor.execute(sql, (species_id, latin, abbrev));
https://stackoverflow.com/questions/65386061/insert-data-into-database-from-input-in-python-using-sql December 21, 2020 at 08:04AM
没有评论:
发表评论