SQL使用pythonn和简化的选择查询插入查询

发布于 2025-02-12 15:35:33 字数 1435 浏览 4 评论 0原文

我有一个sql 插入查询,该查询从用户输入中获取值,还可以从另一个表作为外键插入 id 。因为这是写下以下查询,但似乎不起作用。

status_type表

CREATE TABLE status_type (
ID int(5) NOT NULL,
status varchar(50) NOT NULL
);

信息表

CREATE TABLE info (
ID int(11) NOT NULL,
name varchar(50), NULL
nickname varchar(50), NULL
mother_name varchar(50), NULL
birthdate  date, NULL
status_type int <==this must be the foreign key for the status_type table
create_date date
);

的用户他有一个下拉列表,可从 status_type表 status_type表中检索值强> 在info表中,在哪里进行 int type ,因为我想存储status_type的 id 而不是值

代码:

    query = '''
            INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)
            VALUES(?,?,?,?,?,?,?)
                      
            select t2.ID
            from info as t1
            INNER JOIN status_type as t2
            ON t2.ID = t1.status_type
            '''    
            args = (ID,name,nickname,mother_name,db,status_type,current_date)
                       
   cursor = con.cursor()
   cursor.execute(query,args)
   con.commit()
   st.success('Record added Successfully')

status_type 字段采用int类型(从另一个表中的值ID)。 因此,当用户插入时,它插入值。

我需要的是将此转换为相应的 id 并存储 id

i have an sql insert query that take values from user input and also insert the ID from another table as foreign key. for this is write the below query but it seems not working.

Status_type table

CREATE TABLE status_type (
ID int(5) NOT NULL,
status varchar(50) NOT NULL
);

info table

CREATE TABLE info (
ID int(11) NOT NULL,
name varchar(50), NULL
nickname varchar(50), NULL
mother_name varchar(50), NULL
birthdate  date, NULL
status_type int <==this must be the foreign key for the status_type table
create_date date
);

for the user he has a dropdownlist that retrieve the value from the status_type table in order to select the value that he want to insert into the new record in the info table
where as the info table take int Type because i want to store the ID of the status_type and not the value

code:

    query = '''
            INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)
            VALUES(?,?,?,?,?,?,?)
                      
            select t2.ID
            from info as t1
            INNER JOIN status_type as t2
            ON t2.ID = t1.status_type
            '''    
            args = (ID,name,nickname,mother_name,db,status_type,current_date)
                       
   cursor = con.cursor()
   cursor.execute(query,args)
   con.commit()
   st.success('Record added Successfully')

the status_type field take an INT type (the ID of the value from another table ).
So when the user insert it insert the value.

What i need is to convert this value into its corresponding ID and store the ID

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2025-02-19 15:35:33

根据 @Mastafa NZ的答案,我修改了我的查询,它变成了以下内容:

query = '''
            INSERT INTO info (ID,name,nickname,mother_name,birthdate,status_type,created_date)
            VALUES(?,?,?,?,?,(select status_type.ID 
                        from status_type
                        where status = ?),?)
                      
            '''    
            args = (ID,name,nickname,mother_name,db,status_type,current_date)
                       
   cursor = con.cursor()
   cursor.execute(query,args)
   con.commit()
   st.success('Record added Successfully')

based on the answer of @Mostafa NZ I modified my query and it becomes like below :

query = '''
            INSERT INTO info (ID,name,nickname,mother_name,birthdate,status_type,created_date)
            VALUES(?,?,?,?,?,(select status_type.ID 
                        from status_type
                        where status = ?),?)
                      
            '''    
            args = (ID,name,nickname,mother_name,db,status_type,current_date)
                       
   cursor = con.cursor()
   cursor.execute(query,args)
   con.commit()
   st.success('Record added Successfully')
私野 2025-02-19 15:35:33

创建记录时,您可以以这些方式之一。

  • 接收用户的输入

  • 指定字段的默认值

    插入(...)值(?,?,1,?,?)

  • 中的默认值

    中使用select

    插入(...)value(?,?,(从ide_type oder by ID中选择顶部1 ID),?,?)

在 。 t1.status_type在以下行中是错误的

INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)

When creating a record, you can do one of these ways.

  • Receive as input from the user

  • Specify a default value for the field

    INSERT INTO (...) VALUES (? ,? ,1 ,? ,?)

  • Use a select in the INSERT

    INSERT INTO (...) VALUES (? ,? ,(SELECT TOP 1 ID FROM status_type ODER BY ID) ,? ,?)

When INSERT data, you can only enter the names of the destination table fields. t1.status_type is wrong in the following line

INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文