XLS_CreateFile ich bin verzweifelt

Anfängerfragen zum Programmieren mit PureBasic.
Benutzeravatar
bobobo
jaAdmin
Beiträge: 3857
Registriert: 13.09.2004 17:48
Kontaktdaten:

Re: XLS_CreateFile ich bin verzweifelt

Beitrag von bobobo »

Wenn "nur" Excel-tabellen geschrieben werden sollen ginge das auch über eine ExcelDatenbank (Windows only)
Ich hab da was erhellendes gefunden (nicht von mir) (geht mit x86er Purebasic, mit dem 64er mag es nicht tun). Erzeugt im runverzeichnis eine exceldatei c.xls mit Inhalt
Wenn diese schon da sein sollte gibt es eine Fehlermeldung. Der Zugriff darauf erfolgt über Datenbankzugriff

Irgendwas ist da aber komisch mit der Benamsung dert Tabelle. ?¿? Da muss nmochmal jemand kundiges drüberhuschen

Code: Alles auswählen

;
;
;
;NO UNICODE
;
;
;
;{-  EXCELSCHREIBEN DEFINITIONEN UND PROZEDUREN
;-TOP
; Kommentar : Excel Tabelle lesen über SQL
; Version   : v1.01
; Author    : Michael Kastner
; Datei     : FcDatabase.pb
; Erstellt  : 28.09.2006
; Geändert  :

; ***************************************************************************************

#ODBC_ADD_DSN = 1; // Add data source
#ODBC_CONFIG_DSN = 2; // Configure (edit) Data source
#ODBC_REMOVE_DSN = 3; // Remove data source
#ODBC_ADD_SYS_DSN = 4; // add a system DSN
#ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN
#ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN
#ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN

; ***************************************************************************************

Procedure.s AddDSN(driver.s, databasename.s, user.s, pass.s)
  
  name.s = GetFilePart(databasename.s)
  name = Left(name.s, Len(name.s)-4)
  name = "Pure" + name
  
  Select UCase(driver)
  Case "ACCESS"
    strDriver.s = "Microsoft Access Driver (*.mdb)"
  Case "EXCEL"
    strDriver.s = "Microsoft Excel Driver (*.xls)"
  Default
    strDriver.s = driver
  EndSelect
  
  strAttributes.s = "Server=APServer;Description=" + name
  strAttributes.s + ";DSN=" + name
  strAttributes.s + ";DBQ=" + databasename
  strAttributes.s + ";UID=" + user
  strAttributes.s + ";PWD=" + pass + ";"
  
  MyMemory = AllocateMemory(Len(strAttributes))  ; Allocate the memory you need here
  CopyMemory(@strAttributes, MyMemory, Len(strAttributes))      ; Copy the database information string into the memory space
  
  For L = 1 To Len(strAttributes)                               ; Check the string in the memory space now
    
    If PeekB(MyMemory + L-1) = Asc(";")                       ; If you find a semicolon anywhere in the string
      PokeB(MyMemory + L-1, 0)                                ; Replace it with an empty character as the driver doesn't use it
    EndIf                                                       ; End the current check
    
  Next L                                                        ; Check the next byte
  
  result = SQLConfigDataSource_(0, #ODBC_ADD_DSN, strDriver, MyMemory)   ; Call the function you need from the ODBC library with the right details
  
  FreeMemory(MyMemory)                                     ; Free the memory now
  
  If result
    ProcedureReturn name
  Else
    ProcedureReturn ""
  EndIf
  
EndProcedure

; ***************************************************************************************

Procedure.l RemoveDSN(driver.s, databasename.s, user.s, pass.s)
  
  name.s = GetFilePart(databasename.s)
  name = Left(name.s, Len(name.s)-4)
  name = "Pure" + name
  
  Select UCase(driver)
  Case "ACCESS"
    strDriver.s = "Microsoft Access Driver (*.mdb)"
  Case "EXCEL"
    strDriver.s = "Microsoft Excel Driver (*.xls)"
  Default
    strDriver.s = driver
  EndSelect
  
  strAttributes.s = "DSN=" + name + ";"
  
  MyMemory = AllocateMemory(Len(strAttributes))  ; Allocate the memory you need here
  CopyMemory(@strAttributes, MyMemory, Len(strAttributes))      ; Copy the database information string into the memory space
  
  For L = 1 To Len(strAttributes)                               ; Check the string in the memory space now
    
    If PeekB(MyMemory + L-1) = Asc(";")                       ; If you find a semicolon anywhere in the string
      PokeB(MyMemory + L-1, 0)                                ; Replace it with an empty character as the driver doesn't use it
    EndIf                                                       ; End the current check
    
  Next L                                                        ; Check the next byte
  
  result = SQLConfigDataSource_(0, #ODBC_REMOVE_DSN, strDriver, MyMemory)   ; Call the function you need from the ODBC library with the right details
  
  FreeMemory(MyMemory)                                     ; Free the memory now
  
  ProcedureReturn result
  
EndProcedure
;}


Enumeration
  #db1
EndEnumeration

Path.s=GetTemporaryDirectory()+"\test.xls"

; DSN anlegen
Base.s = AddDSN("Excel", Path, "", "")

Debug "DSN=" + Base
UseODBCDatabase()

If OpenDatabase(#Db1, Base, "", "")
  sql1.s = "create table testit (customer CHAR(40),Nam CHAR(40),country Char(40),rank CHAR(10),month INTEGER)"
  
  
  If DatabaseUpdate(#Db1, sql1)
    
    Debug DatabaseError()
  Else
    Debug DatabaseError()
    MessageRequester("error", "no or exiting table .. no continue .." + #CRLF$ + "going home", 0)
    End
    
  EndIf
  
EndIf


;quick&dirty 1
anum=1234
nam.s="GG"
country.s="France"
rank.s="Enthusiast"
month =6

xl_sqlinsert.s = "Insert into testit values ('" + Str(anum) + "','" + nam + "','" + country + "','" + rank + "','" + Str(month)+ "')"
Debug xl_sqlinsert
DatabaseUpdate(#Db1, xl_sqlinsert)
Debug DatabaseError()

;quick&dirty 2
anum=1235
nam.s="Rook Zimbabwe"
country.s="USA,TX"
rank.s="Addict"
month =1

xl_sqlinsert.s = "Insert into testit values ('" + Str(anum) + "','" + nam + "','" + country + "','" + rank + "','" + Str(month)+ "')"
Debug xl_sqlinsert
DatabaseUpdate(#Db1, xl_sqlinsert)
Debug DatabaseError()

;quick&dirty 3
anum=1236
nam.s="bobobo"
country.s="yes"
rank.s="Idiot"
month =0

xl_sqlinsert.s = "Insert into testit values ('" + Str(anum) + "','" + nam + "','" + country + "','" + rank + "','" + Str(month)+ "')"
Debug xl_sqlinsert
DatabaseUpdate(#Db1, xl_sqlinsert)
Debug DatabaseError()


CloseDatabase(#Db1)

RemoveDSN("Excel", Path, "", "")
Beep_(4000, 125)
Beep_(5320, 250)
RunProgram(path,"","",#PB_Program_Wait)
If MessageRequester("so...","delete %temp%\text.xls?",#PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
  DeleteFile(path)
EndIf

End
‮pb aktuell5.7 - windoof aktuell und sowas von 10
Ich hab Tinnitus im Auge. Ich seh nur Pfeifen.
Antworten