本文介绍了我们如何验证 Sequel.connect 是否在 Rails 中打开了我们的 SQLite3 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们试图找出我们的 Rails 3.1.12 应用程序是否使用 gem 续集打开 SQLite3 数据库(3.6 或更高版本).这是我们所做的:

We are trying to find out if our Rails 3.1.12 app opens the SQLite3 database (version 3.6 or above) with gem sequel. Here is what we did:

  1. rails 控制台
  2. 在 Rails 控制台会话中,键入以下命令:

  1. rails console
  2. In the Rails console session, typed the following command:

sequel = Sequel.connect('sqlite://development')

它返回:

=> #<Sequel::SQLite::Database: "sqlite://development">

还有 sequel.class 返回:

Also sequel.class returns:

=> Sequel::SQLite::Database

但是,当尝试使用 sequel.execute 从数据库中选择或使用 sequel.schema 检查表时,返回的文本表示该表不存在.

However when trying to select from the database with sequel.execute or check a table with sequel.schema, the returned text says that the table does not exist.

我们不太确定数据库(此处的开发)是否已打开.我们如何检查?

We are not quite sure if the database (development here) has been opened or not. How do we check that?

推荐答案

我觉得你可以试试:

DB.test_connection
=> true

test_connection 说:

尝试获取数据库连接.如果成功则返回真.如果不成功,可能会引发错误.如果给出了服务器参数,则尝试获取到给定服务器/分片的数据库连接.

多年前,我们会使用良性查询来判断连接是否有效.你可以试试:

Years ago we'd use a benign query to tell if the connection is alive. You can try:

DB["select datetime('now');"].first

哪个返回:

{
    :"datetime('now')" => "2013-06-25 06:23:44"
}

您可能希望捕获在查询失败时可能引发的任何异常,但这表明连接也已关闭.

You'll probably want to catch any exceptions that might be raised if the query fails, but that would indicate that the connection was down too.

这篇关于我们如何验证 Sequel.connect 是否在 Rails 中打开了我们的 SQLite3 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 14:09