Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135

    Importazione CSV in MySQL

    Ciao a tutti.

    Con uno script ASP importo un file csv in mysql.

    Va tutto bene, ma non capisco perchè nell'inserimento la query registra anche quelli che dovrebbero essere i nomi delle colonne.

    codice:
    D:\Inetpub\wwwroot\PSIC20071031144905.csv
    
    INSERT INTO tbl ( sis, origine, `id sistema` ) VALUES ( 'sis', 'origine', 'idsistema' )
    Sapete aiutarmi?

    codice:
    <%
    
    Server.ScriptTimeout = 10000 
    Response.Buffer = false
    
      Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
      strURL = Server.MapPath("PSIC20071031144905.csv")
    
      Set objFile = objFSO.OpenTextFile(strURL)
      Do While Not objFile.AtEndOfStream
    
      	strText = objFile.readLine
    	arrText = split(strText, ",", 17)
    	
    strSIS = replace(arrText(0), """", " ")
    strORIGINE = replace(arrText(1), """", " ")
    strIDSISTEMA = replace(arrText(2), """", " ")
    	
    strSql = "INSERT INTO " 
    strSql = strSql & " tbl " 
    strSql = strSql & " ( " 
    strSql = strSql & " sis, "
    strSql = strSql & " origine, " 
    strSql = strSql & " `id sistema` "
    strSql = strSql & " ) " 
    strSql = strSql & " VALUES " 
    strSql = strSql & " ( " 
    strSql = strSql & " '"& replace(strSIS, " ", "") & "', " 
    strSql = strSql & " '"& replace(strORIGINE, " ", "") & "', "
    strSql = strSql & " '"& replace(strIDSISTEMA, " ", "") & "' "
    strSql = strSql & " ) " 
    
    cn.execute(strSql)
    
      Loop
      
    
      objFile.Close
      Set objFile = Nothing
      
      cn.Close
      Set cn = nothing
      
      %>
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  2. #2
    Perchè devi iniziare dalla seconda riga del file in poi.
    codice:
    <%
    
    Server.ScriptTimeout = 10000 
    Response.Buffer = false
    
    salta = true
    
      Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
      strURL = Server.MapPath("PSIC20071031144905.csv")
    
      Set objFile = objFSO.OpenTextFile(strURL)
      Do While Not objFile.AtEndOfStream
    
    if not salta then
    
      	strText = objFile.readLine
    	arrText = split(strText, ",", 17)
    	
    strSIS = replace(arrText(0), """", " ")
    strORIGINE = replace(arrText(1), """", " ")
    strIDSISTEMA = replace(arrText(2), """", " ")
    	
    strSql = "INSERT INTO " 
    strSql = strSql & " tbl " 
    strSql = strSql & " ( " 
    strSql = strSql & " sis, "
    strSql = strSql & " origine, " 
    strSql = strSql & " `id sistema` "
    strSql = strSql & " ) " 
    strSql = strSql & " VALUES " 
    strSql = strSql & " ( " 
    strSql = strSql & " '"& replace(strSIS, " ", "") & "', " 
    strSql = strSql & " '"& replace(strORIGINE, " ", "") & "', "
    strSql = strSql & " '"& replace(strIDSISTEMA, " ", "") & "' "
    strSql = strSql & " ) " 
    
    cn.execute(strSql)
    
    else
    
    salta = false
    
    end if
    
      Loop
      
    
      objFile.Close
      Set objFile = Nothing
      
      cn.Close
      Set cn = nothing
      
      %>

  3. #3
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Grazie, ma:

    INSERT INTO tbl ( sis, origine, `id sistema`, `data evento` ) VALUES ( 'sis', 'origine', 'idsistema', 'dataevento' )


    Microsoft OLE DB Provider for ODBC Drivers error '80004005'

    [MySQL][ODBC 3.51 Driver][mysqld-5.0.45-community-nt]Incorrect date value: 'dataevento' for column 'data evento' at row 1


    /Default.asp, line 47
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  4. #4
    i nomi di colonna metti tra [] non tra ''

  5. #5
    code]
    strSql = strSql & " [id sistema] "
    [/code]

  6. #6
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Il DB è MYSQL... le parentesi quadre non sono accettate si sostituiscono con gli apici ` quando il campo della tbl contiene spazi...

    Ho provato così ma non va:

    codice:
    <%
    
    Server.ScriptTimeout = 10000 
    Response.Buffer = false
    
      Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
      strURL = Server.MapPath("PSIC20071031144905.csv")
      
      response.write strURL &"
    
    "
    
      Set objFile = objFSO.OpenTextFile(strURL)
      
    i = 1
    
      Do While Not objFile.AtEndOfStream
      
    if i > 1 then
    
      	strText = objFile.readLine
    	arrText = split(strText, ",", 17)
    	
    strSIS = replace(arrText(0), """", " ")
    strORIGINE = replace(arrText(1), """", " ")
    strIDSISTEMA = replace(arrText(2), """", " ")
    strDATAEVENTO = replace(arrText(3), """", " ")
    
    strSql = "INSERT INTO " 
    strSql = strSql & " tbl " 
    strSql = strSql & " ( " 
    strSql = strSql & " sis, "
    strSql = strSql & " origine, " 
    strSql = strSql & " `id sistema`, "
    strSql = strSql & " `data evento` "
    strSql = strSql & " ) " 
    strSql = strSql & " VALUES " 
    strSql = strSql & " ( " 
    strSql = strSql & " '"& replace(strSIS, " ", "") & "', " 
    strSql = strSql & " '"& replace(strORIGINE, " ", "") & "', "
    strSql = strSql & " '"& replace(strIDSISTEMA, " ", "") & "', "
    strSql = strSql & " '"& replace(strDATAEVENTO, " ", "") & "' "
    strSql = strSql & " ) " 
    
    cn.execute(strSql)
    
    end if
    
       i = i + 1
    
      Loop
      
    
      objFile.Close
      Set objFile = Nothing
      
      cn.Close
      Set cn = nothing
      
      %>
    :master:
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  7. #7
    a sto punto il problema è il contenuto del campo data nel csv

  8. #8
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Originariamente inviato da optime
    a sto punto il problema è il contenuto del campo data nel csv
    Infatti è la prima riga del csv che contiene:

    codice:
    sis,"origine","id sistema","data evento",
    e che devo saltare perchè altrimenti la query va in errore perchè invece di trovarsi i valori delle righe trova i nomi delle colonne:

    codice:
    INSERT INTO tbl ( sis, origine, `id sistema`, `data evento` ) VALUES ( 'sis', 'origine', 'idsistema', 'dataevento' ) 
    
    
    Microsoft OLE DB Provider for ODBC Drivers error '80004005' 
    
    [MySQL][ODBC 3.51 Driver][mysqld-5.0.45-community-nt]Incorrect date value: 'dataevento' for column 'data evento' at row 1
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

  9. #9
    Utente di HTML.it L'avatar di [trodat]
    Registrato dal
    Oct 2004
    Messaggi
    2,135
    Risolto, esiste un apposito comando:

    http://www.w3schools.com/asp/met_skipline.asp


    codice:
     Do While Not objFile.AtEndOfStream
      objFile.SkipLine() 
    Lo Stato dà un posto. L’impresa privata dà un lavoro. – Indro Montanelli

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.