Git Product home page Git Product logo

basic-db-from-schema's Introduction

Address Book Db From Schema

##Learning Competencies

  • Use sqlite gem to create a database using Ruby commands
  • Use sqlite gem to perform CRUD operations on tables in a database using Ruby commands

Summary

Let's build an address book so we can keep track of all the awesome programmers we know.

Revisit (or do if you have not already) the Address Book Schema and write a ruby program to create Objects to represent the address book and create the database address_book.db

##Releases

###Release 0 : Create the address_book database

Create the SQL Schema that represents your database tables. Then, create a file called setup.rb which you will run only once and will create your database. All other parts of your code will assume that the database has already been created.

Next, load some initial records into this database from hardcoded SQL statements. This code should also be in setup.rb.

Extra Credit: Instead of hardcoding your insert statements, create a csv/YAML/JSON file with the data and import it.

###Release 1 : Build the Ruby classes

First, create classes that correspond to your tables, e.g., a "contacts" table corresponds to a Contact class. These classes should have methods that allow you to add, delete, update records in the database.

It should work as follows:

contact = Contact.new(:name => "Alex")
contact.id # nil
contact.save # This executes an INSERT statement and makes a new record
contact.id # e.g., 20

contact.name = "Bob"
contact.save # This executes an UPDATE statement

Note, you will also need to allow your classes to have access to your database. There are several ways to do this, but an easy way would be just to create a global variable that is accessible in all of your classes. (this is fine for now we'll get into more OO approaches later).

$db = SQLite3::Database.new "address_book.db"

###Release 2 : Work with the address_book database using Ruby

Next, create driver code and additional methods in your code that allows you to complete the following tasks.

  1. Add a contact
  2. Add a group
  3. Change a contact address
  4. Delete a contact
  5. Delete a group - careful here - what does this mean? What implications does it (should it) have on your data?

###Release 3 : Data validation Input from users is "Dangerous". What if they give you a phone number with 17 digits? How about an address without a state? How does your database handle this? How does your Ruby code handle this?

These are complex questions. Come up with a plan to work with phone numbers (must have a valid format) and email addresses (must have a valid format and be unique).

Implement this plan in your code.

##Resources

basic-db-from-schema's People

Contributors

aespaldi avatar

Watchers

James Cloos avatar Christina Taggart avatar

Forkers

jepetersohn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.