Skip to main content

Ruby: How To Connect To SQLite Database

Language


In this tutorial we will create A Simple Database Connection Using Ruby/SQLite.Ruby is a dynamic, object-oriented, general-purpose, easy syntax kind of programming language. Most developers find it enjoy to work around with Ruby, and enjoyment is a great motivator for beginners. Ruby display an understandable errors, so you'll still be able to track the problematic part of errors. So let's start coding..

Getting started
First you will have to download & install Ruby, here's the link for the Ruby Modules https://rubyinstaller.org/downloads/.

Installing SQLite
After you installed Ruby, we will now then install the SQLite, open the command prompt then type "gem install sqlite3" and hit enter.

Installing SQLite Browser
After you installed SQLite3, we will now then install the SQLite Browser to view the database data, here's the link for the DB Browser for SQLite.

Importing Modules
After setting up the installation and the database, open any kind of text editor ( notepad++, etc.. ). Then type the code below into the text editor.

  1. #!/usr/bin/ruby
  2.  
  3. #=======================MODULES==========================
  4. require 'tk'
  5. require 'sqlite3'

Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the text editor.

  1. #=======================MAIN=============================
  2. root = TkRoot.new {
  3. title "Ruby: How To Connect To SQLite Database"
  4. width = 400
  5. height = 300
  6. screen_width = winfo_screenwidth()
  7. screen_height = winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. geometry ('%dx%d+%d+%d' % [width, height, x, y])
  11. resizable(0, 0)
  12. }

Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the text editor.

  1. #=======================FRAMES===========================
  2. Top = TkFrame.new(root){
  3. relief 'solid'
  4. borderwidth 1
  5. background 'red'
  6. pack('side' => 'top', 'fill' => 'x')
  7. }
  8.  
  9. List = TkFrame.new(root){
  10. pack('side' => 'top')
  11. }
  12.  
  13. Bottom = TkFrame.new(root){
  14. pack('side' => 'bottom')
  15. }
  16.  
  17. #=======================LIST WIDGET======================
  18.  
  19. $myList = TkListbox.new(List){
  20. relief 'raise'
  21. selectmode 'extended'
  22. height '13'
  23. width '60'
  24. pack('side' => 'left')
  25.  
  26. }
  27.  
  28.  
  29.  
  30. #=======================LABEL WIDGETS====================
  31. $txt_title = TkLabel.new(Top){
  32. background 'red'
  33. foreground 'white'
  34. text "Ruby: How To Connect To SQLite Database"
  35. pack('side' => 'top')
  36. }
  37.  
  38. $lbl_display = TkLabel.new(Bottom){
  39. text
  40. foreground 'green'
  41. pack('side' => 'top')
  42. }
  43.  
  44. $btn_connect = TkButton.new(Bottom){
  45. text 'Connect'
  46. command 'connect'
  47. pack('side' => 'bottom', 'fill' => 'x')
  48. }

Creating the Database Connection
Then after setting up the design we will now create the database function, then it will automatically display data from the database to listbox. To do that just simply copy the code below and paste it inside the text editor.

  1. #=======================METHODS==========================
  2. def connect()
  3. $myList.delete(0, 'end')
  4. conn = SQLite3::Database.new 'rubytut.db'
  5. conn.execute "CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL PRIMARY KEY, fullname TEXT)"
  6. result = conn.execute("SELECT * FROM `member` ORDER BY `mem_id` DESC")
  7. result.each{|row| $myList.insert 0, row[1]}
  8. conn.close()
  9. $lbl_display.configure('text' => 'Connected to database')
  10. end

Initializing the Application
After finishing the function save the application as 'index.rb'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the text editor.

  1. #=======================INITIALIZATION===================
  2. Tk.mainloop()

There you have it we successfully connected the SQLite to Ruby programming Language . I hope that this simple tutorial help you to your projects, For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.