简体   繁体   English

python sqlite 如何使用代码中的附加数据将一行中的一些数据插入到另一张表中

[英]python sqlite how to insert some data from one row into another table with additional data from code

so i need insert first_name and last_name from source table where row contains same uid from handler and insert to recordtable and add record_time.所以我需要从源表中插入first_name和last_name,其中行包含来自处理程序的相同uid并插入到recordtable并添加record_time。

cur.execute("""INSERT INTO recordtable (first_name,
last_name,
record_time)
SELECT first_name, last_name 
FROM sourcetable 
WHERE uid is ?""", ("some string from handler"))

Thats the best i can do but i still need add record_time from handler to that execute, how to do it?这是我能做的最好的,但我仍然需要从处理程序添加record_time到那个执行,怎么做?

here how tables look:这里的表格看起来如何:

CREATE TABLE IF NOT EXISTS sourcetable (
uid TEXT NOT NULL UNIQUE,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL);

CREATE TABLE IF NOT EXISTS recordtable (
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
record_time DATETIME);

Use an INSERT INTO... SELECT statement:使用INSERT INTO... SELECT语句:

INSERT INTO recordtable(first_name, last_name, record_time) 
SELECT first_name, last_name, CURRENT_TIMESTAMP
FROM sourcetable
WHERE uid = ?

never mind, i eventually found out that you can set a predefined values in SELECT operator没关系,我最终发现您可以在 SELECT 运算符中设置预定义值

cur.execute("""INSERT INTO recordtable (first_name, last_name, record_time)
           SELECT first_name, last_name, ? FROM sourcetable WHERE uid is ?;""",
           (datetime.date(datetime.now()), self.__handle_uid))

this is my first experience with sql database for python这是我对 python 的 sql 数据库的第一次体验

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 插入到通过 FOREIGN KEY 链接到第二个表的 2 个表中,并从 sqlite 和 python 中的另一个表中获取数据 - insert into 2 table one linked to the second through FOREIGN KEY and take data from another table in sqlite and python python + sqlite,将变量中的数据插入表中 - python + sqlite, insert data from variables into table 将大量数据从一个表插入到另一个 MySQL Python - Insert bulk of data from one table to another MySQL Python 如何将数据从一个表插入到另一个表? - How to insert data from one table into another table? 如何通过在 Python DataFrame 中保持某些列值不变来将数据从一行合并到另一行 - How do I merge data from one row to another by keeping some column values unchanged in Python DataFrame python sqlite3,使用来自另一个表的数据将行插入表 - python sqlite3, inserting row into table using data from another table SQLITE3 DB数据从同一表和同一行中的一个字段传输到另一个字段 - SQLITE3 DB data transfer from one field to another in the same table and same row sqlite3将主键数据从一个表插入到另一个表 - sqlite3 inserting primary key data from one table into another 如何在 sqlite3 中从自动增量表中插入数据 - How to insert data from an Autoincremented table in sqlite3 获取 Python 代码的帮助,该代码从一个 CSV 文件中获取数据并将其插入到另一个 CSV 文件中 - Get help for Python code that get data from one CSV file and insert it into another CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM