Portal    Foro    Buscar    FAQ    Registrarse    Conectarse


Publicar nuevo tema  Responder al tema 
Página 2 de 2
Ir a la página Anterior  1, 2
 
Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas [Soluc...
Autor Mensaje
Responder citando   Descargar mensaje  
Mensaje Re: Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas 
 
Tincho:


Citar:
y tener una tabla de "aprendizaje" que cuando encuentre "tal cosa" a la base suba Tal Cosa".

Será interesante... puedes usar reglas...

Se puede hacer de varias formas, a lo mejor te sirve este programa que hice:
http://clasificaryordenar.blogspot.com.es/p/presentacion.html

La diferencia que en mi programa eran nombres y extensiones de archivos, y en el tuyo son datos de canciones.
 




===================
Blog personal
Web: SoloGambas seleccion de articulos dedicados a Gambas
Visita el Curso de Gambas3 ¡¡¡Gratuito!!!
 
jsbsan - Ver perfil del usuarioEnviar mensaje privadoVisitar sitio web del usuario 
Volver arribaPágina inferior
Responder citando   Descargar mensaje  
Mensaje Re: Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas 
 
jsbsan escribió: [Ver mensaje]
Será interesante... puedes usar reglas...
Se puede hacer de varias formas, a lo mejor te sirve este programa que hice:
http://clasificaryordenar.blogspot.com.es/p/presentacion.html
La diferencia que en mi programa eran nombres y extensiones de archivos, y en el tuyo son datos de canciones.

Puede ser útil algún tipo de reglas respecto al tratamiento de los nombres de archivos, como en tu programa, o algunas para quitar espacios o caracteres con acento o todo en minúsculas etc. todo lo que acote lo mas posible la repetición de información.
Volveré a echar un vistazo a tu programa.
Saludos.
 



 
tincho - Ver perfil del usuarioEnviar mensaje privado 
Volver arribaPágina inferior
Responder citando   Descargar mensaje  
Mensaje Re: Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas 
 
Luego de unas horas de análisis y de depuración del código logre hacer una función que mete los datos en una tabla teniendo en cuenta el tema de claves foráneas, y pregunta si agrega tal registro a la tabla accesoria si es que no existe (se puede puentear para que inserte a saco).
Lo dejo aca por si a alguien le sirve o le da ganas de mejorarla.

Saludos.

Public Sub ImportFileMetadata(conDat As Connection, strTab As String, stxReff As String[], stxVal As String[])

  Dim tblPri As Table ' Tabla primaria
  Dim tblSec As Table ' Tabla accesoria

  Dim fldPri As Field ' Tabla primaria
  Dim fldSecK As Field ' Campo Clave en tabla accesoria
  Dim fldSecN As Field ' Campo Nombre en tabla accesoria
  Dim strSQL As String
  Dim strSQLf As String
  Dim strSQLv As String
  Dim strSQLr As String ' PAra insertar registros en tablas foráneas

  Dim stxFld As New String[]
  Dim stxFldFK As New String[]
  Dim stxTblFK As New String[] ' matriz de nombres de Tablas foránelas
  Dim stxFldRK As New String[] ' matriz de Campos con las claves foránelas
  Dim stxFldNK As New String[] ' matriz de Campos con los nombres foránelos

  Dim intC As Integer
  Dim intK As Integer
  Dim resChk As Result
  Dim intFK As Integer
  Dim intD As Integer
  Dim resIns As Result
  Dim resUpd As Result

  Dim resInsRef As Result

  Dim strTmp As String

  Dim indIndx As Index ' Campo Nombre en tabla accesoria

  stxFldFK.Clear
  stxTblFK.Clear
  stxFldRK.Clear
  stxFldNK.Clear

  For Each strTmp In stxReff ' Creo las listas FK

    stxFldFK.Add(Split(strTmp, ".")[0])
    stxTblFK.Add(Split(strTmp, ".")[1])
    stxFldRK.Add(Split(strTmp, ".")[2])
    stxFldNK.Add(Split(strTmp, ".")[3])

  Next

  If conDat.Tables.Exist(strTab) = True Then

    tblPri = conDat.Tables[strTab]

    For Each fldPri In tblPri.Fields ' Creo la lista de campos
      stxFld.Add(fldPri.Name)
    Next

    For intC = 0 To stxVal.Max

      intK = stxFldFK.Find(stxFld[intC])

      If intK <> -1 Then ' Si el campo actual es referenciado busco el key en la tabla Foránea

        resChk = conDat.Exec("select " & stxFldRK[intK] & " from " & stxTblFK[intK] & " where " & stxFldNK[intK] & " = '" & stxVal[intC] & "'")

        If resChk.Available Then
          intFK = resChk[stxFldRK[intK]]

          If intFK <> Null Then
            Print stxVal[intC]
            stxVal[intC] = CStr(intFK)
          Endif
        Else ' Quiere decir que el registro no existe en la referencia

          If Message.Question(("El registro") & " " & stxVal[intC] & ": " & ("no existe en la tabla foranea"), ("Insertarlo"), ("Cancelar")) = 1 Then

            strSQLr = "insert into " & stxTblFK[intK] & "(" & stxFldRK[intK] & "," & stxFldNK[intK] & ") values(Null" & ",'" & stxVal[intC] & "')"

            resInsRef = conDat.Exec(strSQLr)
            Wait 0.1

            resChk = conDat.Exec("select " & stxFldRK[intK] & " from " & stxTblFK[intK] & " where " & stxFldNK[intK] & " = '" & stxVal[intC] & "'")

            If resChk.Available Then
              intFK = resChk[stxFldRK[intK]]

              If intFK <> Null Then
                Print stxVal[intC]
                stxVal[intC] = CStr(intFK)
              Endif
            Else ' Quiere decir que el registro no existe en la referencia
              Message.Info(("Tenes que insertar la referencia amano porque\nla forma automatica no funciono"))
            Endif

          Endif

        Endif

      Endif

    Next

  Endif

  ' Una vez que esta todo acomodado verifico si el registro existe o no
  resChk = conDat.Exec("select " & stxFld[1] & " from " & strTab & " where " & stxFld[1] & " = '" & stxVal[1] & "'")
  If resChk.Available Then
    ' El registro ya existe asi que procedo con un update

    resChk = conDat.Exec("select * from " & strTab & " where " & stxFld[1] & " = '" & stxVal[1] & "'")

    For intD = 2 To stxFld.Max
      If stxVal[intD] <> resChk[stxFld[intD]]
        strSQLv &= stxFld[intD] & "='" & stxVal[intD] & "',"
      Endif

    Next

    strSQL = "update " & strTab & " set " & strSQLv & " where " & stxFld[1] & " = '" & stxVal[1] & "'"
    strSQL = Replace(strSQL, "', where", "' where")

    If strSQLv = "" Then
      Print ("No hay cambios para aplicar")
    Else
      resUpd = conDat.Exec(strSQL)
    Endif

  Else
    'El registro no existe, así que procedo con un insert

    For intD = 0 To stxFld.Max

      Select intD
        Case 0
          strSQLf = "insert into " & strTab & "(" & stxFld[intD] & ","
          strSQLv &= "Null" & ","

        Case stxFld.Max
          strSQLf &= stxFld[intD] & ") values("

          Select tblPri.Fields[stxFld[intD]].Type
            Case 4, 7
              strSQLv &= stxVal[intD] & ")"
            Case 9
              strSQLv &= "'" & stxVal[intD] & "')"
          End Select

        Case Else

          strSQLf &= stxFld[intD] & ","

          Select tblPri.Fields[stxFld[intD]].Type

            Case 4, 7

              strSQLv &= stxVal[intD] & ","
            Case 9
              strSQLv &= "'" & stxVal[intD] & "',"
          End Select
      End Select

    Next
    strSQL = strSQLf & strSQLv
    resIns = conDat.Exec(strSQL)

  Endif

End
 

 



 
tincho - Ver perfil del usuarioEnviar mensaje privado 
Volver arribaPágina inferior
Mostrar mensajes anteriores:    
 
OcultarTemas parecidos
Tema Autor Foro Respuestas último mensaje
No hay nuevos mensajes Problema Al Seleccionar Un Registro De La ... Cubel Bases de Datos 5 Martes, 19 Enero 2010, 22:13 Ver último mensaje
Cubel
No hay nuevos mensajes Insertar Un Registro En Una Tabla Con Sqli... Shell Bases de Datos 3 Jueves, 21 Julio 2011, 08:38 Ver último mensaje
shordi
No hay nuevos mensajes Error Al Agregar Registro Sea En Sqlite O ... jousseph Bases de Datos 5 Martes, 28 Agosto 2012, 12:34 Ver último mensaje
jsbsan
No hay nuevos mensajes Sqliteman Y Las Claves Foráneas tincho Bases de Datos 2 Viernes, 09 Septiembre 2016, 15:10 Ver último mensaje
tincho
 

Publicar nuevo tema  Responder al tema  Página 2 de 2
Ir a la página Anterior  1, 2

Usuarios navegando en este tema: 0 registrados, 0 ocultos y 0 invitados
Usuarios registrados conectados: Ninguno


 
Lista de permisos
No puede crear mensajes
No puede responder temas
No puede editar sus mensajes
No puede borrar sus mensajes
No puede votar en encuestas
No puede adjuntar archivos
Puede descargar archivos
No puede publicar eventos en el calendario



  

 

cron