DataBase

Just starting out? Need help? Post your questions and find answers here.
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

DataBase

Post by ClueLess »

can someone help me to find out why is this code not working? It does not write in the database

Code: Select all

Enumeration FormWindow
  #Win_Invoice_input
EndEnumeration

Enumeration FormGadget
  #Txt_Costumer
  #Lbl_Costumer
  #Lbl_Contract_Type
  #Date_Start
  #Txt_Cicle
  #Lbl_Cicle
  #Lbl_Start_Date
  #Opt_Contract_Type
  #Cmd_Save
EndEnumeration

Enumeration DataBase
  #Database  
EndEnumeration

Declare Open_Invoice_Input()
Declare Save_Data()

Procedure Open_Invoice_Input()
  If OpenWindow(#Win_Invoice_input, 0, 0, 500, 160, "Invoice Input", #PB_Window_SystemMenu)
    StringGadget(#Txt_Costumer, 20, 40, 460, 30, "")
    TextGadget(#Lbl_Costumer, 20, 20, 460, 20, "Nome do Cliente")
    TextGadget(#Lbl_Contract_Type, 20, 90, 100, 20, "Tipo de Contrato")
    DateGadget(#Date_Start, 140, 110, 100, 30, "%dd/%mm/%yyyy")
    StringGadget(#Txt_Cicle, 260, 110, 100, 30, "")
    TextGadget(#Lbl_Cicle, 260, 90, 100, 20, "Dias de Ciclo")
    TextGadget(#Lbl_Start_Date, 140, 90, 100, 20, "Data de Inicio")
    ComboBoxGadget(#Opt_Contract_Type, 20, 110, 100, 30)
    ButtonGadget(#Cmd_Save, 380, 110, 100, 30, "Guardar")
  EndIf
EndProcedure

Open_Invoice_Input()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      End

    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Cmd_Save
          Save_Data()
      EndSelect
  EndSelect
ForEver

Open_Invoice_Input()

Procedure Save_Data()
  Costumer.s =  GetGadgetText(#Txt_Costumer)
  StartDate.s = GetGadgetText(#Date_Start)
  Cicle.s =     GetGadgetText(#Txt_Cicle)
  Level.s =     GetGadgetText(#Opt_Contract_Type)
    
  UseSQLiteDatabase()
  
  DatabaseFile.s = "invoicescheduler.s3db"

  If OpenDatabase(#Database, DatabaseFile, "", "")
    
    SQL.s = "INSERT INTO contrats (Costumer, StartDate, Cicle, Level) VALUES ('"
    SQL = SQL + Costumer + "' , '"
    SQL = SQL + SatartDate + "', '"
    SQL = SQL + Cicle + "', '"
    SQL = SQL + Level + "')"
    
    DatabaseUpdate(#Database, SQL)
    If Result = 0
      MessageRequester("ERRO", "Não foi possivel Inserir o registo")
    Else 
      SetGadgetText(#Txt_Costumer, "")
      SetGadgetText(#Txt_Cicle, "")
      SetGadgetText(#Opt_Contract_Type, "")
    EndIf
  Else
    MessageRequester("ERRO", "Não foi possivel abrir a Base de Dados")
  EndIf
EndProcedure

Thank You
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: DataBase

Post by mk-soft »

Code: Select all

result = DatabaseUpdate(#Database, SQL)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: DataBase

Post by ClueLess »

Thank You but this is not the problem

Code: Select all

Enumeration FormWindow
  #Win_Invoice_input
EndEnumeration

Enumeration FormGadget
  #Txt_Costumer
  #Lbl_Costumer
  #Lbl_Contract_Type
  #Date_Start
  #Txt_Cicle
  #Lbl_Cicle
  #Lbl_Start_Date
  #Opt_Contract_Type
  #Cmd_Save
EndEnumeration

Enumeration DataBase
  #Database  
EndEnumeration

Declare Open_Invoice_Input()
Declare Save_Data()

Procedure Open_Invoice_Input()
  If OpenWindow(#Win_Invoice_input, 0, 0, 500, 160, "Invoice Input", #PB_Window_SystemMenu)
    StringGadget(#Txt_Costumer, 20, 40, 460, 25, "")
    TextGadget(#Lbl_Costumer, 20, 20, 460, 20, "Nome do Cliente")
    TextGadget(#Lbl_Contract_Type, 20, 90, 100, 20, "Tipo de Contrato")
    DateGadget(#Date_Start, 140, 110, 100, 25, "%dd/%mm/%yyyy")
    StringGadget(#Txt_Cicle, 260, 110, 100, 25, "")
    TextGadget(#Lbl_Cicle, 260, 90, 100, 20, "Dias de Ciclo")
    TextGadget(#Lbl_Start_Date, 140, 90, 100, 20, "Data de Inicio")
    ComboBoxGadget(#Opt_Contract_Type, 20, 110, 100, 25)
    ButtonGadget(#Cmd_Save, 380, 110, 100, 25, "Guardar")
  EndIf
EndProcedure

Open_Invoice_Input()

Repeat
 Event = WaitWindowEvent()
 
 Select Event
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Cmd_Save
          Save_Data()
      EndSelect
      
  EndSelect
Until Event = #PB_Event_CloseWindow

Open_Invoice_Input()

Procedure Save_Data()
  Costumer.s =  GetGadgetText(#Txt_Costumer)
  StartDate.s = GetGadgetText(#Date_Start)
  Cicle.s =     GetGadgetText(#Txt_Cicle)
  Level.s =     GetGadgetText(#Opt_Contract_Type)
    
  UseSQLiteDatabase()
  
  DatabaseFile.s = "invoicescheduler.s3db"

  If OpenDatabase(#Database, DatabaseFile, "", "")
    
    SQL.s = "INSERT INTO contrats (Costumer, StartDate, Cicle, Level) VALUES ('"
    SQL = SQL + Costumer + "' , '"
    SQL = SQL + SatartDate + "', '"
    SQL = SQL + Cicle + "', '"
    SQL = SQL + Level + "')"
    
    Result = DatabaseUpdate(#Database, SQL)
    If Result = 0
      MessageRequester("ERRO", "Não foi possivel Inserir o registo")
    Else 
      SetGadgetText(#Txt_Costumer, "")
      SetGadgetText(#Txt_Cicle, "")
      SetGadgetText(#Opt_Contract_Type, "")
    EndIf
  Else
    MessageRequester("ERRO", "Não foi possivel abrir a Base de Dados")
  EndIf
EndProcedure


User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: DataBase

Post by Kukulkan »

There is a typo regarding StartDate while composing the SQL (SatartDate).

Depending on your database structure (if there is a constraint on field StartDate that does not allow to be empty), it may be the problem. I suggest you to post a working example incl. the database generation. In case you do not generate the table (CREATE TABLE...), we also found the problem ;-)
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: DataBase

Post by ClueLess »

Thank You

It was not the typo.

I didn't understand what you said regarding the table creation
ClueLess
Enthusiast
Enthusiast
Posts: 345
Joined: Sun Jan 11, 2009 1:04 am

Re: DataBase

Post by ClueLess »

Found the Problem

Costumer was defined as a Primary key and of course was not accepting duplicated fields. The interesting thing is that before I had the error msg with database error() and it was giving me a blank msg

Thank you anyway. You leaded me to the solution
Post Reply