Tutorial: Databases

This tutorial shows us how insert data onto our database

Inserting New Records in Our Table

At this point, we have tables set up to add onto our database. Being able to insert data is one of the most important aspects about databases because a database is meant to hold massive amounts of unique information. To add a new record is similar to adding a new piece of data on a spreadsheet; we want to make sure our data is correctly contained in the correct data column. Here we have a table for student grades and GPA as follows:

GPA Example
StudentID FirstName LastName Grade GPA
1DamianDesmondA4.0
2AnyaForgerA3.8

We want to make sure data types are in the right column so GPA would be a decimal column while first name should be a string column and so fourth. For this tutorial, we will be going back into our grocery list, "grocerylist" table and insert information regarding food. Recall the columns of out table:

Grocery List
FoodID FoodName FoodCost FoodCalories FoodQuantity
DataType: integerDataType: stringDataType: number (double)DataType: integerDataType: integer

We will want to connect our database once again as well as create an Insert object. To do this, we would create a new object called Insert, that allows us to use helper actions to delete the existing table inside of our database.

use Libraries.Data.Database.Database use Libraries.Data.Database.Insert Database database database:Connect("localhost", "myfirstdatabase", "root", "password") Insert insert

Inserting Data Actions
Action / Object Description Usage
Insert objectNameThe Insert query represents the INSERT INTO command of the SQL (Structured Query Language) language. This query allows for data manipulation by inserting new rows into a table.Insert insert
iObject:Add(text columnName, dataType value)Add a column target along with the value that will be associated with it.insert:Add("cost", 4.50) insert:Add("firstName", "Daniel") insert:Add("daysOff", 14)
iObject:SetTable(text tableName)This action sets the table of the query.insert:SetTable("superCoolTable")
iObject:Empty()Removes all columns and values associated with them from the queryinsert:Empty()
dbObject:Insert:(Insert objectName)This takes an Insert query and sends it to the database and returns the result. An Insert allows for adding more rows to a table and if the insert was successful the database will return the number of rows affected. For more information and how to do an insert look into the documentation of the Insert query class.database:Insert(insert)

What we want to do first is select the table we want to insert data using SetTable() and pass in the "grocerylist" table. Once that is complete, all inserts will go into that table and we'll want to add 5 entries of data per row: FoodID, FoodName, FoodCost, FoodCalories, FoodQuantity.

What we want to do first is use SetTable() on our 'insert' object which allows us to select a specific table inside our database. For example, SetTable() takes in one parameter, the table title and in this case we will be passing in "grocerylist" into the action. Then, if we want, we can keep a running sum to see how many rows will be affected in total; we can call this integer value "rowsAffected" and initialize it as 0.

Next, what we will be wanting to do is add in values using Add(); the action add requires two parameters: the column name and the data type value associated with the column. For example, say that we have a column called "firstName," we would want to pass "firstName" in the first parameter and because names are strings, we will insert "Bob" as the second parameter. In our grocerylist table, let's insert 1 for FoodId, Chicken Breast for FoodName, 2.99 for FoodCost, 120 for FoodCalories, and 2 for FoodQuantity using the Add() action.

insert:Add("FoodID", 1)
 insert:Add("FoodName", "Chicken Breast")
 insert:Add("FoodCost", 2.99)
 insert:Add("FoodCalories", 120)
 insert:Add("FoodQuantity", 2)
database:Insert(insert)
 rowsAffected = rowsAffected + 1
 insert:Empty()

We have our data loaded inside this insert object, but it is not actually in our database yet. Using our database object, like previous actions, we will now call in the Insert() action and pass in 'insert' within the parameter. This will update our table in our database in real time and it is also worth noting that Insert() will return either 0 or 1 where 0 means it did not add the row into the database and 1 meaning it has successfully added the row into the database. Now, we want to increment the counter, rowsAffected and then empty our insert object using the action Empty(). It is necessary that before we add another record, we have to Empty() the object or else we will have compilation errors.

Congrats! Now, we can finally multiple entries of data into our table! We can view the entire code below.

use Libraries.Data.Database.Database
use Libraries.Data.Database.Insert

Database database
database:Connect("localhost", "myfirstdatabase", "root", "password")

  Insert insert
  insert:SetTable("grocerylist")
 // keep a running sum on how many rows we add 
 integer rowsAffected = 0
   
  // add first entry
  insert:Add("FoodID", 1)
  insert:Add("FoodName", "Chicken Breast")
  insert:Add("FoodCost", 2.99)
  insert:Add("FoodCalories", 120)
  insert:Add("FoodQuantity", 2)
 // will only insert 1 query at a time
  database:Insert(insert)
  rowsAffected = rowsAffected + 1
    output insert:ToText()

/* clears out info from previous insert
   we need to call this because it only inserts one
   at a time, we can insert multiple queries at 
   the same time with this method
*/
  insert:Empty()
  insert:Add("FoodID", 2)
  insert:Add("FoodName", "Italian Sausage")
  insert:Add("FoodCost", 3.99)
  insert:Add("FoodCalories", 250)
  insert:Add("FoodQuantity", 5)
  database:Insert(insert)
  output insert:ToText()

   rowsAffected = rowsAffected + 1

  insert:Empty()
  insert:Add("FoodID", 3)
  insert:Add("FoodName", "Mango")
  insert:Add("FoodCost", 0.99)
  insert:Add("FoodCalories", 60)
  insert:Add("FoodQuantity", 2)
  database:Insert(insert)
output insert:ToText()

  rowsAffected = rowsAffected + 1

  insert:Empty()
  insert:Add("FoodID", 4)
  insert:Add("FoodName", "Orange Cutie")
  insert:Add("FoodCost", 0.45)
  insert:Add("FoodCalories", 40)
  insert:Add("FoodQuantity", 5)

  database:Insert(insert)
  rowsAffected = rowsAffected + 1
output insert:ToText()

insert:Empty()
  insert:Add("FoodID", 5)
  insert:Add("FoodName", "Banana")
  insert:Add("FoodCost", 0.30)
  insert:Add("FoodCalories", 110)
  insert:Add("FoodQuantity", 7)

  database:Insert(insert)
  rowsAffected = rowsAffected + 1
output insert:ToText()

  output "Inserted " + rowsAffected + " row(s)"

Next Tutorial

In the next tutorial, we will discuss updating databases, which describes how to update data in a database table.