Switch to: V9V8V7V6V5

Lesson 2 - "Employees" Database on Server

In Lesson 1, you learned how to create a local, Valentina-powered database application with LiveCode.

In Lesson 2, you will learn how to work with local databases as well as remote databases located on the Valentina Server.

In order to follow this lesson, you should have the project created in Lesson 1.

Step 1: Project Preparation for the Work with Server

Modifying Initialization

Copy the files you created in Lesson1_step6 to a new location, start LiveCode, then load the project.

In the current stack, create a new custom property called gClient. This is used in the openCard handler. To do this you should do the next:

  1. On the toolbar, click Inspector.
  2. In the Inspector, Select the Custom tab. Create a new custom property called gClient, set the Value to true.
  3. Go back to your card and select Edit Script. Modify the code you openCard handler as follows:
on openCard
    if the gClient of this stack is true then
        get valentina_InitClient()
    else
        get valentina_Init( 10 * 1024 * 1024 )
    end if
    DatabaseCreate
end openCard

Modifying the DatabaseCreate handler

Instead of creating a database locally, you should create a database on the remote server. Locate the DatabaseCreate handler from Lesson 1 and modify the script as:

on DatabaseCreate
  if the gClient of this stack is true then
    put VConnection_Constructor( "localhost", "sa", "sa") into mConn
    get VConnection_Open( mConn )

    put "Database1.vdb" into dbPath

    put VDatabase_Constructor( mConn ) into mDatabase
   
  else
      -- Build db full path near to stack:
      get the effective filename of this stack
      set the itemDel to slash
      put the (item 1 to -2 of it) & slash & "Database1.vdb" into dbPath

    put VDatabase_Constructor() into mDatabase
  end if
  
  get VDatabase_Open( mDatabase, dbPath )
   
  if VDatabase_ErrNumber( mDatabase ) is not "0" then
    if VDatabase_ErrNumber( mDatabase ) is not "532480" then 
      get VDatabase_Create( mDatabase, dbPath, "kDscDatBlbInd" )
      CreateStructure
    else
      answer VDatabase_ErrString( mDatabase )
      close the defaultStack
    end if
  end if
   
  get VDatabase_DateFormat( mDatabase, "kYMD" ) 
  get VDatabase_DateSep( mDatabase, "-" )  

  put VDatabase_Table( mDatabase, "Person" ) into mPerson
  put VTable_Field( mPerson, "Name" )  into mName
  put VTable_Field( mPerson, "LName" )  into mLName
  put VTable_Field( mPerson, "BirthDate" )  into mBirthDate

end DatabaseCreate

If Valentina Server is located on another computer, you should change “localhost” to the address of your server.

Preparing Valentina Server

Before you can use your project, Valentina Server has to be running.

Run the code you have so far. If you followed all the tasks, Valentina creates a database where the server stores databases and not locally in a folder.

Step 2: Adding error checking

Adding new function ShowError()

To do this you should do the following:

  • Choose “Edit Script” from the card popup menu.
  • Write the following text:
function ShowError
  put Valentina_ErrNumber() into errCode
  put Valentina_ErrString() into errString
  
  if errCode <> 0 then 
    show message box
    put errString into message
  end if
end ShowError

Improving DatabaseCreate() procedure

Locate the DatabaseCreate handler modify the script as

on DatabaseCreate
  if the gClient of this stack is true then
    put VConnection_Constructor( "localhost", "sa", "sa") into mConn
    get VConnection_Open( mConn )
    get ShowError()
    
    put VDatabase_Constructor( mConn ) into mDatabase
  else
    put VDatabase_Constructor() into mDatabase
  end if
  
  get VDatabase_Open( mDatabase, "Database1" )
  get ShowError()  
  
  ...