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 http://sqlitebrowser.org/. 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. #!/usr/bin/ruby #=======================MODULES========================== require 'tk' 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. #=======================MAIN============================= root = TkRoot.new { title "Ruby: How To Connect To SQLite Database" width = 400 height = 300 screen_width = winfo_screenwidth() screen_height = winfo_screenheight() x = (screen_width/2) - (width/2) y = (screen_height/2) - (height/2) geometry ('%dx%d+%d+%d' % [width, height, x, y]) resizable(0, 0) } 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. #=======================FRAMES=========================== Top = TkFrame.new(root){ relief 'solid' borderwidth 1 background 'red' pack('side' => 'top', 'fill' => 'x') } List = TkFrame.new(root){ pack('side' => 'top') } Bottom = TkFrame.new(root){ pack('side' => 'bottom') } #=======================LIST WIDGET====================== $myList = TkListbox.new(List){ relief 'raise' selectmode 'extended' height '13' width '60' pack('side' => 'left') } #=======================LABEL WIDGETS==================== $txt_title = TkLabel.new(Top){ background 'red' foreground 'white' text "Ruby: How To Connect To SQLite Database" pack('side' => 'top') } $lbl_display = TkLabel.new(Bottom){ text foreground 'green' pack('side' => 'top') } $btn_connect = TkButton.new(Bottom){ text 'Connect' command 'connect' pack('side' => 'bottom', 'fill' => 'x') } 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. #=======================METHODS========================== def connect() $myList.delete(0, 'end') conn = SQLite3::Database.new 'rubytut.db' conn.execute "CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER NOT NULL PRIMARY KEY, fullname TEXT)" result = conn.execute("SELECT * FROM `member` ORDER BY `mem_id` DESC") result.each{|row| $myList.insert 0, row[1]} conn.close() $lbl_display.configure('text' => 'Connected to database') 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. #=======================INITIALIZATION=================== 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!!

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.

Tags

Add new comment