Ruby Programming Tutorial - Lesson 33 - Creating Key Value Store DB - JokeDB
ruby·@bilal-haider·
0.000 HBDRuby Programming Tutorial - Lesson 33 - Creating Key Value Store DB - JokeDB
## In this article we are going to create our Key-Value Store DB In previous articles we learned to use many data structures but we were not able to persist data, as soon as we closed our program, data is lost. We learned to work with files so now, its time that we create our first persistent database. ## The Task > Create a persistent database or a key-value store, that has features as follows, 1 - Create new databases 2 - Select a database 3 - Insert Data There are many Databases/keyvalue stores available with much more features than our JokeDB but we wanted to create something of our own. so lets get started. Just in case you want to use some of professional key-value stores, check these out  For people who want to use JokeDB lets first make it :) ## Identifying Entities - Database - Data In our example we are just creating a Database class, because that is enough for our purpose, but you can extend this jokeDB to create amazing features into it and use it as needed. We start by creating a Class called Jokedb, which has three methods - Initialize - Insert - Show_content ``` class Jokedb attr_accessor :dbFile def initialize(name) _name = name+".dat" if !File::exists?("jokedb/"+_name) Dir.mkdir('jokedb') unless Dir.exist?('jokedb') @dbFile = File.new("jokedb/"+_name, "w") else @dbFile = File.open("jokedb/"+_name) end #File.open(name) if File::exists?(name) end def show_content IO.foreach(@dbFile){|block| puts block} end def insert(key, value) open(@dbFile, 'a') { |f| f << "[\"#{key}\",\"#{value}\"]\n" puts "Data Inserted: [\"#{key}\",\"#{value}\"]" } end end ``` ## 1 - Initialize Initialize method is used to Create a db directory, and inside that directory a new database file. - If there exists a database file already, it will open that and use it - If there exists no database file, with the specified name, It will create a new file and use that  ## 2 - Insert Second method opens database file, and appends it the text which is passed to it, as key and value. You should also know that, while use this in code you can also pass a HASH to it as a value which is cool :) but for our purpose, we will store a key, and a value ## 3 - show content This method allows you to see the content of the database, e.g all the records Later on we can implement more features to our JokeDB, so that it can also search for a record, it should also delete a record, it can also sort a record .. but for now we are going with just these few methods. Lets now create main.rb file and use our JokeDB there ``` require_relative 'jokedb' while $entry.to_i != 5 print "1 - Create New Database \n2 - Select Database\n3 - Insert Data\n4 - Show Data\n5 - Exit\nYour Choice: " $entry = gets.chomp if $entry.to_i == 1 print "Insert Database Name : " _dbname = gets.chomp _myDatabase = Jokedb.new(_dbname) elsif $entry.to_i == 2 print "Select Database : " _dbname = gets.chomp $myDatabase = Jokedb.new(_dbname) elsif $entry.to_i == 3 print "Insert key : " _key = gets.chomp print "Insert Value : " _value = gets.chomp $myDatabase.insert(_key, _value) elsif $entry.to_i == 4 $myDatabase.show_content() elsif $entry.to_i == 5 puts "Exiting .... " else puts "Invalid choice " end end ``` Create a Database  Notice that, after I typed Database name, it creates a jokedb folder, and puts a database file in it.  As soon as my Database is created I can then select it, and Start inserting data  Notice that I can also create another database, and insert data into it  #### In this article we learned to create a Key-value Store that can help us persist our data, its not ideal because if you delete the file your data is gone.. as long as you can protect this file your data is safe
👍 bilal-haider, ashe-oro, nanocheeze, jdcrunchman, thebluespirit, esteemboard, alfatih2726, udinleo, marcoteixeira, mygs2018, miraj, chapui87, team, androidmaster, rmdochi14, anwar21, emperatriz1503, dhofer, svenis, hashmieali77, juninho, naserdear, thriftytraveller, donghyun, indahelyamani, derpdragon, smritisc, thecreepyfiles, cryptopieta, rosmiati93.awh,