|
Página 2 de 2
|
Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas [Soluc...
Autor |
Mensaje |
jsbsan
Analista Programador
Registrado: Septiembre 2009
Mensajes: 4175
Edad: 51 Ubicación: dos hermanas, sevilla
|
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.
|
#11 Viernes, 09 Septiembre 2016, 20:30 |
|
|
tincho
Analista Programador
Registrado: Noviembre 2014
Mensajes: 2565
Edad: 48 Ubicación:
|
Re: Insertar Un Registro En Sqlite En Una Tabla Que Usa Claves Foráneas
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.
|
#12 Sabado, 10 Septiembre 2016, 16:12 |
|
|
tincho
Analista Programador
Registrado: Noviembre 2014
Mensajes: 2565
Edad: 48 Ubicación:
|
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
|
#13 Sabado, 10 Septiembre 2016, 16:16 |
|
|
|
Temas parecidos
Temas parecidos
|
Página 2 de 2
|
Usuarios navegando en este tema: 0 registrados, 0 ocultos y 0 invitados Usuarios registrados conectados: Ninguno
|
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
|
|
|
|
|