Calificación:
  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
como guardar las filas de una tabla en una sola fila de otra tabla
#1
buen dia,

quisiera saber como colocar varias filas de una tabla de base de datos sqlite3 en una sola fila de otra tabla ejemplo:

tengo una tabla de productos con su precio en la otra tengo varios vendedores donde esta su nombre y apellidos pueden ser varios vendedores por producto cuando el usuario ingresa un producto puede agregar varios vendedores de un combobox

tabla1                                                                 
productos  precio
mouse        120
teclado       200
laptop         1000

tabla2
nombre          apellidos
diana              collazos
juan                perez
luis                  gonzalez
jhon                 rairez


tabla resultante
productos   precio    vendedor1nombre vendedor1Apellido  vendedor2nombre   vendedor2apellido   vendedor3nombre  vendedor3apellido                         
mouse          120                    diana           collazos                          luis                     gonzalez                        null                  null
teclado          200                    jhon             rairez                               null                    null                               null                null
laptop         10000                   juan            perez                            diana                  collazos                        jhon               rairez
Responder
#2
Hola.

Te convendría crear una tabla donde asocies productos con vendedores, en lugar de crear una columna para cada vendedor. Algo así:

Código:
CREATE TABLE vendedores_por_producto (producto_id INTEGER, vendedor_id INTEGER)

Si tenés varios vendedores (supongamos, vendedores con id 1, 2 y 3) por producto (por ejemplo, producto con id 1), simplemente insertás varias filas:

Código:
INSERT INTO vendedores_por_producto VALUES (1, 1);
INSERT INTO vendedores_por_producto VALUES (1, 2);
INSERT INTO vendedores_por_producto VALUES (1, 3);

Saludos
¡No te pierdas nuestro curso oficial en Udemy para aprender Python, bases de datos SQL, orientación a objetos, tkinter y mucho más!

También ofrecemos consultoría profesional de desarrollo en Python para personas y empresas.
Responder
#3
hola francisco lo que realmente deseo hacer es que necesito que la seleccion de un item en treeview me lo inserte en las columnas de otra tabla aqui te dejo el codigo que guarde en las columnas de la tabla dotoes1

Código:
def ventana_principal():
    global ventana5
    ventana5=Tk()
    ventana5.title("sesion_iniciada")
    ventana5.geometry("1370x700")
    global matriculaNuevo_entry
    global cedulaNuevo_entry
    global precioNuevo_entry
    global cedulavendedor_entry
    global primerApellido1_entry
    global segundoApellido1_entry
    global primerNombre1_entry
    global segundoNombre1_entry
    global sexo1_entry
    global fecha_nacimiento1
    global rh1_entry
    global domicilio1_entry
    global estadocivil1_entry
    global cedulacomprador_entry
    global primer_apellido2_entry
    global segundo_apellido2_entry
    global primer_nombre2_entry
    global segundo_nombre2_entry
    global sexo2_entry
    global fecha_nacimiento2
    global rh2_entry
    global domicilio2_entry
    global estadocivil2_entry
    global tabla
    global tabla1
   
   Label(ventana5, text="Matricula:",  font=("Arial Black", 11)).place(x=25, y=55)
    matriculaNuevo_entry=Entry(ventana5, textvariable="matricula_nuevo")
    matriculaNuevo_entry.place(x=115, y=60)
    Label(ventana5, text="Cedula Catastral:", font=("Arial Black", 11)).place(x=270, y=55)
    cedulaNuevo_entry=Entry(ventana5, textvariable="cedula_nuevo")
    cedulaNuevo_entry.place(x=420, y=60)
    Label(ventana5, text="Precio:", font=("Arial Black", 11)).place(x=25, y=95)
    precioNuevo_entry=Entry(ventana5, textvariable="precio_nuevo")
    precioNuevo_entry.place(x=90, y=100)

    ttk.Button(ventana5, text="Enviar", cursor="hand2", command=enviar_datos).place(relx=0.50, rely=0.18)
    Label(ventana5, text="Vendedores", bg="deep sky blue", fg="white", width=140, height=1, font=("calibri", 14)).place(x=0, y=155)
    Label(ventana5, text="C.C", font=("Arial Black", 9)).place(x=75, y=190)
    cedulavendedor_entry=Entry(ventana5, textvariable="cedulavendedor")
    cedulavendedor_entry.place(x=25, y=210)
    Label(ventana5, text="Primer Apellido", font=("Arial Black", 9)).place(x=180, y=190)
    primerApellido1_entry=Entry(ventana5, textvariable="primerApellido1")
    primerApellido1_entry.place(x=170, y=210)
    Label(ventana5, text="Segundo Apellido", font=("Arial Black", 9)).place(x=325, y=190)
    segundoApellido1_entry=Entry(ventana5, textvariable="segundoApellido1")
    segundoApellido1_entry.place(x=320, y=210)
    Label(ventana5, text="Primer Nombre", font=("Arial Black", 9)).place(x=480, y=190)
    primerNombre1_entry=Entry(ventana5, textvariable="primerNombre1")
    primerNombre1_entry.place(x=470, y=210)
    Label(ventana5, text="Segundo Nombre", font=("Arial Black", 9)).place(x=620, y=190)
    segundoNombre1_entry=Entry(ventana5, textvariable="segundoNombre1")
    segundoNombre1_entry.place(x=620, y=210)
    Label(ventana5, text="Sexo", font=("Arial Black", 9)).place(x=767, y=190)
    sexo1_entry=Entry(ventana5, textvariable="sexo1", width=5)
    sexo1_entry.place(x=770, y=210)
    Label(ventana5, text="Fecha de Nacimie", font=("Arial Black", 9)).place(x=820, y=190)
    fecha_nacimiento1=Entry(ventana5)
    fecha_nacimiento1.place(x=820, y=210)
    Label(ventana5, text="RH", font=("Arial Black", 9)).place(x=960, y=190)
    rh1_entry=Entry(ventana5, width=5)
    rh1_entry.place(x=960, y=210)
    Label(ventana5, text="Domicilio", font=("Arial Black", 9)).place(x=1040, y=190)
    domicilio1_entry=Entry(ventana5, textvariable="domicilio1")
    domicilio1_entry.place(x=1010, y=210)
    Label(ventana5, text="Estado Civil", font=("Arial Black", 9)).place(x=1200, y=190)
    estadocivil1_entry=ttk.Combobox(ventana5, state="roadonly", textvariable="estadocivil1", width=30, values=["Soltero sin union marital", "soltero con union marital", "Casado conyugal vigente",
    "Casado conyugal disuelta y liquidada", "Viudo conyugal sin liquidar"])
    estadocivil1_entry.place(x=1150, y=210)
    tabla = ttk.Treeview(ventana5, height=6,  columns=[f"#{n}" for n in range(1, 8)])
    tabla.place(x=15, y=235, width=1340)
    tabla.heading("#0", text = "Cedula", anchor = CENTER)
    tabla.heading("#1", text = "Primer Apellido", anchor = CENTER)
    tabla.heading("#2", text = "Segundo Apellido", anchor = CENTER)
    tabla.heading("#3", text = "Primer Nombre", anchor = CENTER)
    tabla.heading("#4", text = "Segundo Nombre", anchor = CENTER)
    tabla.heading("#5", text = "Sexo", anchor = CENTER )
    tabla.heading("#6", text = "Domicilio", anchor = CENTER)
    tabla.heading("#7", text = "Estado civil", anchor = CENTER )
    tabla.column("#0", width=30, anchor = CENTER)
    tabla.column("#1", width=30, anchor = CENTER)
    tabla.column("#2", width=30, anchor = CENTER)
    tabla.column("#3", width=30, anchor = CENTER)
    tabla.column("#4", width=30, anchor = CENTER)
    tabla.column("#5", width=10, anchor = CENTER )
    tabla.column("#6", width=30, anchor = CENTER)
    tabla.column("#7", width=50, anchor = CENTER)
    ttk.Button(ventana5, text="Insertar vendedor", cursor="hand2", command=insertarvendedores).place(rely=0.54, relwidth=1)
    Label(ventana5, text="Compradores", bg="deep sky blue", fg="white", width=140, height=1, font=("calibri", 14)).place(x=0, y=415)
    Label(ventana5, text="C.C", font=("Arial Black", 9)).place(x=75, y=450)
    cedulacomprador_entry=Entry(ventana5, textvariable="cedula_comprador")
    cedulacomprador_entry.place(x=25, y=470)
    Label(ventana5, text="Primer Apellido", font=("Arial Black", 9)).place(x=180, y=450)
    primer_apellido2_entry=Entry(ventana5, textvariable="primer_apellido2")
    primer_apellido2_entry.place(x=170, y=470)
    Label(ventana5, text="Segundo Apellido", font=("Arial Black", 9)).place(x=325, y=450)
    segundo_apellido2_entry=Entry(ventana5, textvariable="segundo_apellido2")
    segundo_apellido2_entry.place(x=320, y=470)
    Label(ventana5, text="Primer Nombre", font=("Arial Black", 9)).place(x=480, y=450)
    primer_nombre2_entry=Entry(ventana5, textvariable="primer_nombre2")
    primer_nombre2_entry.place(x=470, y=470)
    Label(ventana5, text="Segundo Nombre", font=("Arial Black", 9)).place(x=620, y=450)
    segundo_nombre2_entry=Entry(ventana5, textvariable="segundo_nombre2")
    segundo_nombre2_entry.place(x=620, y=470)
    Label(ventana5, text="Sexo", font=("Arial Black", 9)).place(x=767, y=450)
    sexo2_entry=Entry(ventana5, textvariable="sexo2", width=5)
    sexo2_entry.place(x=770, y=470)
    Label(ventana5, text="Fecha de Nacimie", font=("Arial Black", 9)).place(x=820, y=450)
    fecha_nacimiento2=Entry(ventana5)
    fecha_nacimiento2.place(x=820, y=470)
    Label(ventana5, text="RH", font=("Arial Black", 9)).place(x=960, y=450)
    rh2_entry=Entry(ventana5, width=5)
    rh2_entry.place(x=960, y=470)
    Label(ventana5, text="Domicilio", font=("Arial Black", 9)).place(x=1040, y=450)
    domicilio2_entry=Entry(ventana5, textvariable="domicilio2")
    domicilio2_entry.place(x=1010, y=470)
    Label(ventana5, text="Estado Civil", font=("Arial Black", 9)).place(x=1200, y=450)
    estadocivil2_entry=ttk.Combobox(ventana5, state="roadonly", width=30, values=["Soltero sin union marital", "soltero con union marital", "Casado conyugal vigente",
    "Casado conyugal disuelta y liquidada", "Viudo conyugal sin liquidar"])
    estadocivil2_entry.place(x=1150, y=470)
    tabla1 = ttk.Treeview(ventana5, height=6,  columns=[f"#{n}" for n in range(1, 8)])
    tabla1.place(x=15, y=495, width=1340)
    tabla1.heading("#0", text = "Cedula", anchor = CENTER)
    tabla1.heading("#1", text = "Primer Apellido", anchor = CENTER)
    tabla1.heading("#2", text = "Segundo Apellido", anchor = CENTER)
    tabla1.heading("#3", text = "Primer Nombre", anchor = CENTER)
    tabla1.heading("#4", text = "Segundo Nombre", anchor = CENTER)
    tabla1.heading("#5", text = "Sexo", anchor= CENTER )
    tabla1.heading("#6", text = "Domicilio", anchor = CENTER)
    tabla1.heading("#7", text = "Estado civil", anchor= CENTER )
    tabla1.column("#0", width=30, anchor = CENTER)
    tabla1.column("#1", width=30, anchor = CENTER)
    tabla1.column("#2", width=30, anchor = CENTER)
    tabla1.column("#3", width=30, anchor = CENTER)
    tabla1.column("#4", width=30, anchor = CENTER)
    tabla1.column("#5", width=10, anchor = CENTER)
    tabla1.column("#6", width=30, anchor = CENTER)
    tabla1.column("#7", width=50, anchor = CENTER)
    ttk.Button(ventana5, text="Insertar Comprador", cursor="hand2", command=insertarcompradores).place(rely=0.91,                relwidth=1)

  ventana5.mainloop()

def enviar_datos():
    bd=sqlite3.connect("login1.db")
    mcursor = bd.cursor()
           
    mcursor.execute("CREATE TABLE datoes1  VALUES  (matriculaNuevo TEXT, cedulaNuevo_entry TEXT(), precioNuevo_entry TEXT, direccionNueva_entry.TEXT, dia_comb TEXT(), mes_comb TEXT, año_comb TEXT, ubicacion TEXT, adquirir TEXT, rural_urbano TEXT, notaria TEXT, escritura TEXT, notario TEXT, departamento TEXT, municipio TEXT, primerApellido1_entry TEXT,segundoApellido1 TEXT, primerNombre1, segundoNombre1, sexo1, domicilio1, estadocivil1, cedulacomprador_entry.get()), values=(primer_apellido2_entry.get(), segundo_apellido2_entry.get(), primer_nombre2_entry.get(), segundo_nombre2_entry.get(), sexo2_entry.get(), domicilio2_entry.get(), estadocivil2_entry.get()")  
    mcursor.execute("INSERT INTO datoes1  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (matriculaNuevo_entry.get(), cedulaNuevo_entry.get(), precioNuevo_entry.get(), direccionNueva_entry.get(), dia_comb.get(), mes_comb.get(), año_comb.get(), ubicacion.get(), adquirir.get(), rural_urbano.get(), notaria.get(), escritura.get(), notario.get(), departamento.get(), municipio.get(), primerApellido1_entry.get(),segundoApellido1_entry.get(), primerNombre1_entry.get(), segundoNombre1_entry.get(), sexo1_entry.get(), domicilio1_entry.get(), estadocivil1_entry.get(), cedulacomprador_entry.get()), values=(primer_apellido2_entry.get(), segundo_apellido2_entry.get(), primer_nombre2_entry.get(), segundo_nombre2_entry.get(), sexo2_entry.get(), domicilio2_entry.get(), estadocivil2_entry.get()))
    bd.commit()
   
    messagebox.showinfo(message="Registro exitoso", title="Aviso")
    bd.close()
def insertarvendedores():
    bd=sqlite3.connect("login1.db")
    mcursor = bd.cursor()
       
    #mcursor.execute("CREATE TABLE vendedores1 (CC TEXT, Primer_Apellido TEXT, Segundo_Apellido TEXT, Primer_Nombre TEXT, Segundo_Nombre Text, Sexo TEXT, Domicilio TEXT, Estado_Civil TEXT, Fecha_nacimineto TEXT, Rh TEXT, )")
    mcursor.execute("INSERT INTO vendedores1  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (cedulavendedor_entry.get(), primerApellido1_entry.get(), segundoApellido1_entry.get(), primerNombre1_entry.get(), segundoNombre1_entry.get(), sexo1_entry.get(), domicilio1_entry.get(), estadocivil1_entry.get(), fecha_nacimiento1.get(), rh1_entry.get()))  
    tabla.insert("", 0, text=(cedulavendedor_entry.get()), values=( primerApellido1_entry.get(),segundoApellido1_entry.get(), primerNombre1_entry.get(), segundoNombre1_entry.get(), sexo1_entry.get(), domicilio1_entry.get(), estadocivil1_entry.get()))  
    bd.commit()
    messagebox.showinfo(message="Vendedor registrado exitosamente", title="Aviso")
    cedulavendedor_entry.delete(0, END)
    primerApellido1_entry.delete(0, END)
    segundoApellido1_entry.delete(0, END)
    primerNombre1_entry.delete(0, END)
    segundoNombre1_entry.delete(0,END)
    sexo1_entry.delete(0, END)
    domicilio1_entry.delete(0, END)
    estadocivil1_entry.delete(0, END)
    rh1_entry.delete(0, END)
    fecha_nacimiento1.delete(0, END)
    bd.close()
def insertarcompradores():
    bd=sqlite3.connect("login1.db")
    mcursor = bd.cursor()
           
    mcursor.execute("INSERT INTO compradores1  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (cedulacomprador_entry.get(), primer_apellido2_entry.get(), segundo_apellido2_entry.get(), primer_nombre2_entry.get(), segundo_nombre2_entry.get(), sexo2_entry.get(), domicilio2_entry.get(), estadocivil2_entry.get(), fecha_nacimiento2.get(), rh2_entry.get() ))
    tabla1.insert("", 0, text=(cedulacomprador_entry.get()), values=(primer_apellido2_entry.get(), segundo_apellido2_entry.get(), primer_nombre2_entry.get(), segundo_nombre2_entry.get(), sexo2_entry.get(), domicilio2_entry.get(), estadocivil2_entry.get()))
    bd.commit()
    messagebox.showinfo(message="Comprador registrado exitosamente", title="Aviso")
    cedulacomprador_entry.delete(0, END)
    primer_apellido2_entry.delete(0, END)
    segundo_apellido2_entry.delete(0, END)
    primer_nombre2_entry.delete(0, END)
    segundo_nombre2_entry.delete(0,END)
    sexo2_entry.delete(0, END)
    domicilio2_entry.delete(0, END)
    estadocivil2_entry.delete(0, END)
    rh2_entry.delete(0, END)
    fecha_nacimiento2.delete(0, END)
    bd.close()
ventana_principal()
Responder


Salto de foro:


Usuarios navegando en este tema: 1 invitado(s)