我在两个独立的sqlite3数据库中有两个表.数据类型相同,但模式略有不同.我希望它们是单个数据库中的单个表,具有与表2相同的模式
表格1
CREATE TABLE temp_entries ( id INTEGER PRIMARY KEY,sensor NUMERIC,temp NUMERIC,date NUMERIC);
表2
CREATE TABLE "restInterface_temp_entry" ( "id" integer NOT NULL PRIMARY KEY,"dateTime" integer NOT NULL,"sensor" integer NOT NULL,"temp" integer NOT NULL );
id在两个表之间不是唯一的.我想创建另一个具有与表2相同的模式的表.我希望表1中的条目的id从0开始,然后表2中的条目从表1的最后一个条目开始.
理想情况下,我只想将表1中的条目添加到表2并“重新索引”主键,使其与“dateTime”的升序相同.
更新:我现在有两个表使用相同的模式,我通过创建一个与表2相同的模式的新表到保存表1的数据库中做到这一点.我将数据复制到新表中,例如:
INSERT INTO restInterface_temp_entry(id,dateTime,sensor,temp) ...> select id,date,temp FROM temp_entries;
背景
我曾经将一堆temp_entries记录到csv文件中.我想将数据放入一种更易于使用的格式并选择sqlite3.我编写了一个程序,将所有条目都删除并将它们放入表1中.我不确定当时我在做什么,并使用表2来表示所有新条目.现在我想将它们全部组合起来,希望按升序保持id和日期.
解决方法
弄清楚了.
ATTACH '/orig/db/location' as orig
insert into orig.restInterface_temp_entry(dateTime,temp) ...> select dateTime,temp from main.restInterface_temp_entry;
>清除当前数据库表
delete from main.restInterface_temp_entry where id > 0
>将原始数据库表中的所有更新记录复制回当前.
insert into main.restInterface_temp_entry(id,temp) ...> select id,temp ...> from orig.restInterface_temp_entry;