简体   繁体   中英

Split table into 2 new tables by index with sqlite3 and python

I have a database.db file that has a table1 like thine one. Note that the index is just codes an not numeric:

id    | item | price
-------------
 45f5 | book | 20  
 25h8 | copy | 30   
 as34 | pen  | 10 
 t674 | key  | 15 
 5h6f | ring | 25 
 67yu | mug  | 40 

and I would like to create two additional tables in my dababase.db names table2 and table3 in wich one contain the first 4 rows and the other one the last 2 rows:

table2

id    | item | price
-------------
 45f5 | book | 20  
 25h8 | copy | 30   
 as34 | pen  | 10 
 t674 | key  | 15 

table3

id    | item | price
-------------
 5h6f | ring | 25 
 67yu | mug  | 40 

I have been trying with CREATE TABLE but I have too many columns in table1 to write it one by one. What would be your approach to this problem? Thanks!

"CREATE TABLE table2 AS SELECT * FROM table1 WHERE condition"

To create table you have to specify name of column and type, SELECT * FROM already exisitig will not work. See https://www.w3schools.com/sql/sql_create_table.asp

You can try to create such table based on already existing table like:

INSERT INTO table2  SELECT * FROM table1 WHERE condition; 

Try to use above statement without WHERE to check whether it works as I don't have access now to SQLite database to check it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM