Ruby MySQL输出在不同服务器上冲突

时间:2022-10-21 23:40:53

I have coded a Ruby IRC bot which is on github (/ninjex/rubot) which is having some conflicting output with MySQL on a dedicated server I just purchased.

我编写了一个Ruby IRC bot,它位于github(/ ninjex / rubot)上,它在我刚刚购买的专用服务器上与MySQL有一些冲突的输出。

Firstly we have the connection to the database in the MySQL folder (in .gitignore) which looks similar to the following code block.

首先,我们在MySQL文件夹(在.gitignore中)与数据库建立连接,该文件夹类似于以下代码块。

@con = Mysql.new('localhost', 'root', 'pword', 'db_name')

Then we have an actual function to query the database

然后我们有一个查询数据库的实际函数

def db_query
 que = get_message # Grabs query from user i.e,./db_query SELECT * FROM words
 results = @con.query(que) # Send query through the connection i.e, @con.query("SELECT * FROM WORDS")
 results.each {|x| chan_send(x)} # For each row returned, send it to the channel via
end

On my local machine, when running the command:

在我的本地计算机上,运行命令时:

./db_query SELECT amount, user from words WHERE user = 'Bob' and word = 'hello'

./db_query SELECT amount,用户来自单词WHERE user ='Bob'和word ='hello'

I receive the output in IRC in an Array like fashion: ["17", "Bob"] Where 17 is amount and Bob is the user.

我在IRC中以类似时尚的方式接收输出:[“17”,“Bob”]其中17是金额,Bob是用户。

However, using this same function on my dedicated server results in an output like: 17Bob I have attempted many changes in the code, as well as try to parse the data into it's own variable, however it seems that 17Bob is coming out as a single variable, making it impossible to parse into something like an array, which I could then use to send the data correctly.

但是,在我的专用服务器上使用相同的功能会产生如下输出:17Bob我在代码中尝试了很多更改,并尝试将数据解析为它自己的变量,但似乎17Bob作为单个出现变量,使得无法解析为数组之类的东西,然后我可以使用它来正确地发送数据。

This seems odd to me on both my local machine and the dedicated server, as I was expecting the output to first send 17 to the IRC and then Bob like:

这对我来说在我的本地机器和专用服务器上都很奇怪,因为我期望输出首先向IRC发送17然后Bob就像:

17
Bob

For all the functions and source you can check my github /Ninjex/rubot, however you may need to install some gems.

对于所有功能和来源,你可以检查我的github / Ninjex / rubot,但是你可能需要安装一些宝石。

1 个解决方案

#1


1  

A few notes:

几点说明:

  1. Make sure you are sanitizing query via get_message. Or you are opening yourself up to some serious security problems.
  2. 确保通过get_message清理查询。或者你正在打开一些严重的安全问题。

  3. Ensure you are using the same versions of the mysql gem, ruby and MySql. Differences in any of these may alter the expected output.
  4. 确保您使用的是相同版本的mysql gem,ruby和MySql。任何这些的差异都可能改变预期的产出。

  5. If you are at your wits end and are unable to resolve the underlying issue, you can always send a custom delimiter and use it to split. Unfortunately, it will muck up the case that is actually working and will need to be stripped out.
  6. 如果您处于智慧结束并且无法解决根本问题,您始终可以发送自定义分隔符并使用它进行拆分。不幸的是,它会破坏实际工作的情况并且需要被剥离。

Here's how I would approach debugging the issue on the dedicated machine:

以下是我在专用机器上调试问题的方法:

def db_query
 que = get_sanitized_message
 results = @con.query(que)
 require 'pry'
 binding.pry

 results.each {|x| chan_send(x)}
end
  1. Add the pry gem to your Gemfile, or gem install pry.
  2. 将pry gem添加到Gemfile或gem install pry。

  3. Update your code to use pry: see above
  4. 更新您的代码以使用pry:见上文

  5. This will open up a pry console when the binding.pry line is hit and you can interrogate almost everything in your running application.
  6. 当绑定binding.pry行时,这将打开一个pry控制台,您可以查询正在运行的应用程序中的几乎所有内容。

  7. I would take a look at results and see if it's an array. Just type results in the console and it will print out the value. Also type out results.class. It's possible that query is returning some special result set object that is not an array, but that has a method to access the result array.
  8. 我会看看结果,看看它是否是一个数组。只需在控制台中输入结果,它就会打印出值。同时输入results.class。查询可能返回一些非数组的特殊结果集对象,但它有一个访问结果数组的方法。

  9. If results is an array, then the issue is most likely in chan_send. Perhaps it needs to be using something like puts vs print to ensure there's a new line after each message. Is it possible that you have different versions of your codebase deployed? I would also add a sleep 1 within the each block to ensure that this is not related to your handling of messages arriving at the same time.
  10. 如果结果是一个数组,那么问题很可能在chan_send中。也许它需要使用诸如puts vs print之类的东西来确保在每条消息之后有一个新行。是否可能部署了不同版本的代码库?我还会在每个块中添加一个sleep 1,以确保这与您对同时到达的消息的处理无关。

#1


1  

A few notes:

几点说明:

  1. Make sure you are sanitizing query via get_message. Or you are opening yourself up to some serious security problems.
  2. 确保通过get_message清理查询。或者你正在打开一些严重的安全问题。

  3. Ensure you are using the same versions of the mysql gem, ruby and MySql. Differences in any of these may alter the expected output.
  4. 确保您使用的是相同版本的mysql gem,ruby和MySql。任何这些的差异都可能改变预期的产出。

  5. If you are at your wits end and are unable to resolve the underlying issue, you can always send a custom delimiter and use it to split. Unfortunately, it will muck up the case that is actually working and will need to be stripped out.
  6. 如果您处于智慧结束并且无法解决根本问题,您始终可以发送自定义分隔符并使用它进行拆分。不幸的是,它会破坏实际工作的情况并且需要被剥离。

Here's how I would approach debugging the issue on the dedicated machine:

以下是我在专用机器上调试问题的方法:

def db_query
 que = get_sanitized_message
 results = @con.query(que)
 require 'pry'
 binding.pry

 results.each {|x| chan_send(x)}
end
  1. Add the pry gem to your Gemfile, or gem install pry.
  2. 将pry gem添加到Gemfile或gem install pry。

  3. Update your code to use pry: see above
  4. 更新您的代码以使用pry:见上文

  5. This will open up a pry console when the binding.pry line is hit and you can interrogate almost everything in your running application.
  6. 当绑定binding.pry行时,这将打开一个pry控制台,您可以查询正在运行的应用程序中的几乎所有内容。

  7. I would take a look at results and see if it's an array. Just type results in the console and it will print out the value. Also type out results.class. It's possible that query is returning some special result set object that is not an array, but that has a method to access the result array.
  8. 我会看看结果,看看它是否是一个数组。只需在控制台中输入结果,它就会打印出值。同时输入results.class。查询可能返回一些非数组的特殊结果集对象,但它有一个访问结果数组的方法。

  9. If results is an array, then the issue is most likely in chan_send. Perhaps it needs to be using something like puts vs print to ensure there's a new line after each message. Is it possible that you have different versions of your codebase deployed? I would also add a sleep 1 within the each block to ensure that this is not related to your handling of messages arriving at the same time.
  10. 如果结果是一个数组,那么问题很可能在chan_send中。也许它需要使用诸如puts vs print之类的东西来确保在每条消息之后有一个新行。是否可能部署了不同版本的代码库?我还会在每个块中添加一个sleep 1,以确保这与您对同时到达的消息的处理无关。