ruby访问数据库_Ruby中的数据库访问

ruby访问数据库

Ruby数据库访问 (Ruby Database Access)

If you want to connect your Ruby code with a database, you will have to take help from DBI. DBI is an abbreviation for Database Independent Interface and facilitates you with a conceptual layer between the Ruby code and primary database. It allows you an easy switching of Database implementations and also determines a set of variables, methods, and standard that advances a compatible database interface which is independent of the actual database being utilized.

如果要将Ruby代码与数据库连接,则必须从DBI获得帮助。 DBI是“ 数据库独立接口”的缩写,它在Ruby代码和主数据库之间提供了一个概念层。 它使您可以轻松切换数据库实现,还可以确定一组变量,方法和标准,以推进兼容的数据库接口,该接口独立于所使用的实际数据库。

如何安装和获取Ruby DBI? (How to install and obtain Ruby DBI?)

For downloading the Ruby DBI, click on the link given below,

要下载Ruby DBI,请点击以下链接,

https://imgur.com/NFEuWe4/embed

https://imgur.com/NFEuWe4/embed

After successful downloading, follow the steps given below before carrying out the process of Installation and also make sure the root privilege of your machine.

成功下载后,在执行安装过程之前,请按照以下步骤操作,并确保计算机的root特权。

Step 1:

第1步:

    $ tar zxf dbi-0.2.0.tar.gz

You are required to step into the distribution directory namely dbi-0.2.0 and setup.rb script will let you configure it. With the help of the following the command, the entire driver will be installed by default.

您需要进入分发目录,即dbi-0.2.0,setup.rb脚本将允许您对其进行配置。 借助以下命令,默认情况下将安装整个驱动程序。

Step 2:

第2步:

    $ ruby setup.rb config

Finally, build the driver and install the same by writing the following commands on the terminal.

最后,通过在终端上编写以下命令来构建驱动程序并进行安装。

Step 3:

第三步:

    $ ruby setup.rb setup
    $ ruby setup.rb install

Here we are going to work with MySQL database. So, obviously before getting connected to a database, first, we have to create it. To create a database with the following specifications,

在这里,我们将使用MySQL数据库。 因此,显然在连接数据库之前,首先,我们必须创建它。 要创建具有以下规范的数据库,

  • The name of database is ExampleDB.

    数据库的名称为ExampleDB。

  • There must exists a table named EMPLOYEE in ExampleDB.

    ExampleDB中必须存在一个名为EMPLOYEE的表。

  • Table must be having fields namely FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.

    表格必须包含FIRST_NAME,LAST_NAME,AGE,SEX和INCOME字段。

  • The database should be accessible to Username 'user123' with the Password 'dbtest'.

    用户名“ user123”和密码“ dbtest”应该可以访问该数据库。

  • The proper installation of Ruby Module 'DBI' is must; otherwise no connection will be established.

    必须正确安装Ruby Module'DBI' ; 否则将无法建立连接。

  • You should be aware of basics of MySQL.

    您应该了解MySQL的基础知识。

Now, let us see how to connect a Ruby code with the Database with the help of following code,

现在,让我们看看如何在以下代码的帮助下将Ruby代码与数据库连接,

require "dbi"
begin
   # The following statement is the statement 
   # to get connected with the Database
   datab = DBI.connect("DBI:Mysql:ExampleDB:localhost", "user123", "dbtest")
   # retrieving server version and displaying it.
   row = datab.select_one("SELECT VERSION()")
   puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
   puts "There is a problem with the connection. Error occured"
   puts "Error code:    #{e.err}"
   puts "Error message: #{e.errstr}"
ensure
   # disconnecting from server
   datab.disconnect if datab
end

The above code will display the Server version. Firstly, we are including the module dbi in the code. If the connection is successfully established then the statement will return a database handle and it will get stored into the datab. Otherwise, an exception is thrown and datab will be assigned a nil value. The error code and statement will be printed in the terminal. At last, it is required to close the connection for releasing the resources held by the program.

上面的代码将显示服务器版本。 首先,我们在代码中包含模块dbi 。 如果成功建立连接,则该语句将返回数据库句柄,并将其存储到datab中 。 否则,将引发异常,并且datab将被分配为nil值。 错误代码和声明将显示在终端中。 最后,需要关闭连接以释放程序所拥有的资源。

翻译自: https://www.includehelp.com/ruby/database-access.aspx

ruby访问数据库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值