使用sqlite3和python将表拆分为2个新表

时间:2018-10-13 11:12:22

标签: python sql sqlite

我有一个database.db文件,其中有一个像您的table1。请注意,索引只是编码而不是数字:

import tkinter as tk
from tkinter import ttk

def main():
    def get_txt():
        lab2.config(text=ent.get())

    # x = tk.StringVar()
    x.set("default entry text")

    # y = tk.StringVar()
    y.set("default combo option")

    ent = ttk.Entry(root) # this is where the ttk has to be changed to tk for the default text to show up
    ent.config(textvariable=x)
    lab = ttk.Label(root, textvariable=x)
    lab2 = ttk.Label(root)
    buttn = ttk.Button(root, text='GET TEXT', command=get_txt)
    combo = ttk.Combobox(root, values=['dog', 'cat', 'goldfish'], textvariable=y) # there's no tk.Combobox
    lab3 = ttk.Label(root, textvariable=y)

    ent.grid()
    lab.grid()
    lab2.grid()
    buttn.grid()
    combo.grid()
    lab3.grid()

root = tk.Tk()
x = tk.StringVar()
y = tk.StringVar()
main()
root.mainloop()

并且我想在dababase.db中创建两个额外的表,名称table2和table3,其中一个包含前4行,另一个包含后2行:

table2

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

table3

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

我一直在尝试使用CREATE TABLE,但是table1中的列太多,无法一一写入。您将如何解决此问题?谢谢!

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

1 个答案:

答案 0 :(得分:0)

要创建表,您必须指定列名和类型, SELECT * FROM已经存在将无法使用。参见https://www.w3schools.com/sql/sql_create_table.asp

您可以尝试根据已存在的表创建此类表,例如:

INSERT INTO table2  SELECT * FROM table1 WHERE condition; 

尝试使用上面没有WHERE的语句来检查它是否正常工作,因为我现在无法访问SQLite数据库进行检查。