- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Doing SQL queries with Ruby in SQLite
We have already established a connection to the database. Now we are going modify and fetch the data from the database.
Data is retrieved from the database with the SELECT
statement. In SQLite Ruby module, first we prepare the SQL statement with the prepare
method. The SQL string is sent to the database engine, which checks the statement validity, syntax and in some databases also the user permissions to perform certain queries. If all is OK, a statement object is returned to the Ruby script. The next step is the call to the execute
method. The method executes the query within the database. The data is retrieved.
The Ruby SQLite module has several methods to fetch data from database tables. After the SQL statement was prepared and executed, we can go through the returned data.
Fetching data
In the first example we fetch one row from the Cars
table.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.new "test.db" id = 1 stm = db.prepare "SELECT * FROM Cars WHERE Id=?" stm.bind_param 1, id rs = stm.execute row = rs.next puts row.join "\s" rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure stm.close if stm db.close if db end
In the example we do all the steps to get the first row from the Cars
table.
stm = db.prepare "SELECT * FROM Cars WHERE Id=?"
The SELECT
statement is prepared with the prepare
method. A statement object is returned.
stm.bind_param 1, id
A parameter is bound to the placeholder in the statement.
rs = stm.execute
The statement is executed. A ResultSet
object is returned.
row = rs.next
We obtain the next row from the result set. Since we want to fetch only one row, we call the next
method once.
puts row.join "\s"
The row is a Ruby array. The three fields are joined with a space character to form a line using the join
method.
$ ./fetch.rb 1 Audi 52642
This is the output of the example.
In the following example, we will fetch five rows. We put the next
method in a while loop.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" stm = db.prepare "SELECT * FROM Cars LIMIT 5" rs = stm.execute while (row = rs.next) do puts row.join "\s" end rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure stm.close if stm db.close if db end
In this script we connect to the database and fetch 5 rows of the Cars
table.
stm = db.prepare "SELECT * FROM Cars LIMIT 5"
This is the SQL statement for fetching 5 rows.
while (row = rs.next) do puts row.join "\s" end
The next
method is put inside the while loop. It returns the next row from the result set. If no more rows are left, the method returns nil and the while loop is terminated.
We can get data from the result set using the each
method.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" stm = db.prepare "SELECT * FROM Cars LIMIT 5" rs = stm.execute rs.each do |row| puts row.join "\s" end rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure stm.close if stm db.close if db end
Again we select five rows from the Cars
table.
rs.each do |row| puts row.join "\s" end
We use the each
method to iterate over the result set.
The next example shows the database object's execute
method. It is a convenience method that saves a few keystrokes.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" rows = db.execute "SELECT * FROM Cars LIMIT 5" for row in rows do puts row.join "\s" end rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure db.close if db end
The example selects and prints five rows from the Cars
table.
rows = db.execute "SELECT * FROM Cars LIMIT 5"
Here we do two jobs in one step. We prepare the statement and execute it. The method returns the data in a Ruby array.
for row in rows do puts row.join "\s" end
We print the data from the Ruby array.
So far we have seen data returned in the form of a ResultSet
or an array. The next example will return the data in the form of an array of hashes. This way we can identify field values by their column names.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" db.results_as_hash = true ary = db.execute "SELECT * FROM Cars LIMIT 5" ary.each do |row| printf "%s %s %s\n", row['Id'], row['Name'], row['Price'] end rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure db.close if db end
In the example we get fields by their column names.
db.results_as_hash = true
We set the results_as_hash
property to true. All rows will be returned as Hash objects, with the column names as the keys.
ary.each do |row| printf "%s %s %s\n", row['Id'], row['Name'], row['Price'] end
We get the fields by their column names.
$ ./fetch_hash.rb 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000
We see the output of the example.
Fetching a row or a value
Ruby SQLite module has two convenience methods for retrieving a row or a value. In the first example, we will get a single row from a table.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" row = db.get_first_row "SELECT * FROM Cars WHERE Id=1" puts row.join "\s" rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure db.close if db end
We get the data for the first row of the Cars
table.
row = db.get_first_row "SELECT * FROM Cars WHERE Id=1"
The get_first_row
method gets the first row and discards all other rows.
puts row.join "\s"
The row is printed to the console.
$ ./fetchrow.rb 1 Audi 52642
Here we see the output of the fetchrow.rb
example.
In the last example, we select a single value.
#!/usr/bin/ruby require 'sqlite3' begin db = SQLite3::Database.open "test.db" val = db.get_first_value "SELECT Price FROM Cars WHERE Name='Bentley'" puts val rescue SQLite3::Exception => e puts "Exception occurred" puts e ensure db.close if db end
We select a price for a specific car.
val = db.get_first_value "SELECT Price FROM Cars WHERE Name='Bentley'"
With the get_first_value
method we select a specific field of a row. In our case it is the price of the Bentley car.
$ ./fetchvalue.rb 350000
This is the output.
In this part of the SQLite Ruby tutorial, we have demonstrated how to fetch data from the database using various methods.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论