Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    [ADO] stringaSQL troppo lunga?

    Salve. Sto convertendo un vecchissimo Db ad accesso casuale in access (ado).
    Il problema che mi si presenta (mi sembra di capire) è che usando la query sql per inserire i vari record, questa risulta essere troppo lunga e quindi mi esce un errore di sintassi. Infatti convertendo meno campi (stringaSQL più corta) funziona. Ci sono altri metodi per scrivere i record senza usare una stringa SQL oppure qualche altra soluzione?

    Connessione al DB:

    Private Sub ConnectToDB()
    Set MioDbConn = New ADODB.Connection
    MioDbConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
    & "Data Source=" _
    & OttieniPercorsoApplic _
    & "pratiche.mdb"
    MioDbConn.Open
    Set MioDbCmd = New ADODB.Command
    Set MioDbCmd.ActiveConnection = MioDbConn
    MioDbCmd.CommandType = adCmdText
    End Sub

    Routine di conversione:

    Private Sub Command6_Click()
    Dim CampoID As Integer
    Dim a, strSQL As String
    Dim OldDb As vecchiapratica
    Open OttieniPercorsoApplic & "pratiche.008" For Random Access Read As 1 Len = Len(OldDb)
    CampoID = 0
    For a = 1 To LOF(1) / 112
    Get #1, a, OldDb
    CampoID = CampoID + 1
    strSQL = "INSERT INTO tabella( ID"
    strSQL = strSQL & ", pratica"
    strSQL = strSQL & ", viaggio"
    strSQL = strSQL & ", nave"
    strSQL = strSQL & ", bandiera"
    strSQL = strSQL & ", codice"
    strSQL = strSQL & ", data"
    strSQL = strSQL & ", porto"
    strSQL = strSQL & ", agenzia"
    strSQL = strSQL & ", valuta1"
    strSQL = strSQL & ", valuta2"
    strSQL = strSQL & ", cambionave1"
    strSQL = strSQL & ", cambionave2"
    strSQL = strSQL & ", finita"
    strSQL = strSQL & ", nuovo"
    strSQL = strSQL & ", numsubpra"
    strSQL = strSQL & ") VALUES ("
    strSQL = strSQL & CampoID
    strSQL = strSQL & ", '" & Replace$(OldDb.prat, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.viaggio, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.nave, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.bandiera, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.codice, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.data, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.port, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.agen, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.valuta1, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.valuta2, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.cambionave1, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.cambionave2, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.finita, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.vuoto, "'", "''") & "'"
    strSQL = strSQL & ", '" & Replace$(OldDb.NMSP, "'", "''") & "'"
    strSQL = strSQL & ")"
    MioDbCmd.CommandText = strSQL
    MioDbCmd.Execute
    Next a
    Close #a
    End Sub

    Ringrazio anticipatamente, Fabio.

  2. #2
    Utente di HTML.it
    Registrato dal
    Dec 2002
    Messaggi
    1,326
    ciao,

    hai provato cosi:

    [code]
    For a = 1 To LOF(1) / 112
    Get #1, a, OldDb
    CampoID = CampoID + 1
    MioDbCmd.CommandText = "INSERT INTO tabella( ID"
    MioDbCmd.CommandText = MioDbCmd.CommandText & ", pratica"
    .....
    [/cod]

  3. #3
    Ciao,

    questa sezione del forum riguarda esclusivamente i database (sql, problemi di installazione e/o configurazione e simili) non il codice in VB6/VB.NET.

    Ti sposto nella sezione più appropriata, però fa più attenzione la prossima volta.
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  4. #4
    Mi scuso con il moderatore per il post nella sezione sbagliata.
    Per quanto riguarda il mio problema ringrazio 123delphi321, ma anche così non funziona.
    Se la stringa finale è sotto i 254 caratteri funziona anche come dice 123delphi321, ma come si aggiungono altri dati alla stringa mi da errore di sintassi. Ho provato anche a scomporla del tipo:

    strSQL1 = "INSERT INTO tabella( ID"
    strSQL1 = strSQL & ", pratica"
    etc...
    strSQL2 = ", viaggio"
    strSQL2 = strSQL2 & ", nave"
    etc...

    MioDbCmd.CommandText = strSQL1 & strSQL2 & strSQL3 & strSQL4
    MioDbCmd.Execute

    ma non funziona nemmeno così :-(

  5. #5
    ti posso assicurare che non è un problema di lunghezza

    ho scritto query anche da 700 righe di codice ed oltre e non ho mai avuto di questi problemi

    è probabile che ci sia un errore di sintassi proprio nella zona dei campi che vai a togliere
    The fastest Redis alternative ... cachegrand! https://github.com/danielealbano/cachegrand

  6. #6
    Grazie daniele_dll, è vero... non ci sono limiti di lunghezza. Ho trovato il problema. Probabilmente in alcuni campi del vecchio db non ci sono dati e questo creava l'errore. E' bastato mettere nei replace$ un valore di uno spazio vuoto e tutto si è risolto. Mi hai tolto quest'atroce dubbio sulla lunghezza. Grazie di nuovo.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.