【问题标题】:Insert data into database from input in python using sql使用sql从python中的输入将数据插入数据库
【发布时间】:2020-12-21 00:04:26
【问题描述】:

假设我在名为data.db 的数据库中有下表:

CREATE TABLE species 
(
    species_id int PRIMARY KEY,    -- NCBI taxonomic code
    abbrev character(2),
    latin  character varying(50),
    common character varying(20)
);

如何使用 SQL 从 Python 手动将新数据添加到此数据库中?

输入函数应该如下所示:

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:

我试过的代码是这样的:

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()

编辑 - 根据反馈更新

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));

【问题讨论】:

  • 请用您正在运行的数据库标记您的问题:mysql、oracle、postgres...?
  • 更新了该信息
  • Sqlite和mysql是两种不同的数据库产品,不同的sql语法。删除了冲突的产品标签。请添加您实际使用的那个!
  • 我用的是sqlite3,但是没有use标签。不过sqlite标签是存在的,所以我加了。
  • 你有什么想法@Shadow

标签: python sql sqlite sql-insert


【解决方案1】:

在 python 和 SQLite 中,你会这样做:

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));

这修复了 SQL 语法,并使用了准备好的语句。请注意,不需要fetchinsert 语句,因为它不返回结果集。也无需将null 值分配给未为插入提供的列;你可以忽略它们。

【讨论】:

  • 这给出了:TypeError: data_add() 缺少 4 个必需的位置参数:'cursor'、'species_id'、'latin' 和 'abbrev'
  • @JohnDoe:值列表周围缺少括号。固定。
  • 我仍然收到相同的类型错误消息。有什么想法吗?
  • 关于如何继续的任何想法?
【解决方案2】:
def data_add(conn, cursor):
    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):")
    
    sql = "INSERT INTO species (species_id, latin, abbrev) VALUES ('{species_id}', '{latin}', '{abbrev}', NULL);".format(species_id=A, latin=B, abbrev=C)

    cursor.execute(sql,[A], [B], [C])
    conn.commit()

由于您从原始用户输入中获取了species_id、latin、abbrev,我认为没有理由将它们作为函数参数包含在内。还要留意你的 SQL 语法。

但是,仅 Python 并不能提供连接到数据库的方法。您需要使用像 pyodbc 这样的库。

【讨论】:

  • 我正在使用“import sqlite3”连接,所以这可能不是问题。
  • 您的代码给了我以下错误:TypeError: data_add() missing 1 required positional argument: 'cursor'
  • 这里 conn 和 cursor 是你的两个位置参数。那些(并且只有那些)需要在调用时传递给函数。 conn 参数需要是 sqlite3 连接对象,而 cursor 需要是 sqlite3 游标对象。详情请参阅sqlite3 docs
  • 当然可以,但是当我同时拥有 conn 和 cursor 时,我无法执行该函数。然后我收到此错误消息。我只需要选择一个或以不同的方式更改代码。
猜你喜欢
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-22
  • 2012-01-31
  • 1970-01-01
相关资源
最近更新 更多