Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    Database - Collection Creation/Removal

    2D Blocks
    1
    1
    203
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      Introduction

       
      Database blocks are used to store data on the CreatiCode server, which can be retrieved later.

      For example, suppose you are building an app that collects user inputs, similar to a Google Form. Any user can open your project, but when they input some data, that data can not be saved as part of the project, since they can not save your project. With the help of a database, your app can store the user’s input in the database. After all the users have submitted their data using the app, you can read all the data from the database and further process it.

       
       

      Create a Collection

       
      To use a database, you first need to create a collection. A collection is very similar to a table, with a few columns that describe what kind of data is stored, and many rows of actual data.

      To create a collection, open the “database” category, and click the “Make a Collection” button. You will need to give the collection a name, and then add one or more columns. For each column, you need to specify the name of that column and what type of data will be stored in that column (date, text or number).

      Here is how to create a collection named “students” with 3 columns of “name”, “age” and “height”:
       

      db.gif

       
      Note that after creating this collection, a dozen new blocks will appear to allow us to work with this collection.

      Also, you will see a “preview” of this new collection in the stage window. It will contain the 3 columns, but there is no data. After we add more data to the collection, the top few rows of that collection will be displayed in this preview.

       
       

      A New Table is Also Created

       

      When the “students” collection is created, a new table named “students_table” is created automatically. This table works the same way as any other table, but it has the same columns as the collection, so it can be used to add data to the collection or read from the collection.

      You can turn on the display of this table using its checkbox like this:

      d3.gif

       

      Do not confuse this table with the collection preview. The “students_data” table is a real table: you can change it manually or use the blocks in the “table” category. The “preview” is a read-only display of the top few rows of data stored in the collection in the database.

      A typical workflow is like this: you add some data to the table “students_data”, then you insert that data into the “students” collection, then you will see that data in the preview of that collection. See “Add Data to a Collection” below.

       
       

      Change Collection Columns

       
      After a collection is created, you can still make changes to it, such as changing the collection name or some column’s name, or adding/removing columns. To do that, right-click on the collection’s name, and select “Change collection”:

       
      db2.gif

       
      Note that when you change the columns of a collection, all existing data will be removed, so usually you should only make such changes before adding any actual data to it.

       
       

      Delete a Collection

       
      It is fairly straightforward to delete a collection. You can just right-click on the collection’s name and select the “delete” menu option:

      e58b1bdb-74e3-4a72-8945-23ef47a2b977-image.png

       
       

      Add Data to a Collection

       
      To add some data into the “students” collection, we do not do it directly. Instead, we first add the new data into the “students_data” table, then insert data from that table into the collection.

      For example, we can first manually add 2 rows of data to the table:
       

      d5.gif

       
      Next, we can use the “insert” block in the “database” category to insert the data from the “students_data” table into the “students” collection:

      d6.gif

       
      Note that the preview of the “students” collection now shows 2 rows of data in there. Although they look the same as the 2 rows in the “students_data” table, they are stored in the database server, not as part of this project. This way, any user can open this project and use the “insert” block to add more data to the same collection without changing this project.

      1 Reply Last reply Reply Quote 1
      • First post
        Last post