Multiple MySQL queries with Ruby - mysql

I'm having troubles with MySQL queries in Ruby. I use 'mysql' gem.
Configuration is stored in a separate yml file and loaded into #conf variable.
This is my code:
# connect to the database
Mysql::new(#conf['sql_host'], #conf['sql_user'], #conf['sql_password'], #conf['sql_base'])
# it's ok when we're doing this
my.query("SELECT * FROM `my_table`") do |e|
# code
end
# Maybe, I've missed something here...
# really this query will insert value into other table, used SELECT just for testing
# this throws exception: 'query: not connected'
my.query("SELECT * FROM `my_table_2`")
Windows XP
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
mysql (2.8.1, 2.7.3)
MySQL client version: 5.0.51a
Second query throws 'query: not connected'.

First of all, your program looks ok. I am 100% sure you have libmysql.dll version problem.
I can reproduce this with libmysql.dll provided from Mysql installer.
Download this file and replace in c:\ruby\bin\
http://instantrails.rubyforge.org/svn/trunk/InstantRails-win/InstantRails/mysql/bin/libmySQL.dll
and re-run your program without any change.
Related issue and credit to here

I have run into immeasurable problems when attempting to use Ruby's mysql gem on any version of Windows. From what I can tell, it simply doesn't work unless you can figure out how to compile it yourself (which is a royal pain in the ass).
Have you considered using ActiveRecord as your ORM (object relational mapping) layer and doing development with SQLite or some other database on Windows and then running the production environment on Linux with MySQL? This is the solution that I took a while back and it worked out quite well.

Related

Broken MySql driver for PySide2 on Windows?

I've been forced to try to port to PySide2 from PySide because I'm developing code (on Windows 7) used inside Maya and Maya 2017 has moved to PySide2.
PySide2 ships with Maya but only comes with the driver to talk to sqlite DBs. So I need to tell Qt about a new DIR that contains the MySql driver, like this:
from PySide2 import QtWidgets
QtWidgets.QApplication.addLibraryPath("C:\temp\plugins")
This DIR contains a sub sqldrivers DIR which contains the driver. This works- when I call QtSql.QSqlDatabase.drivers() the new one is there so I've got the right driver and it's being picked up.
However when I create a new DB and call open:
db = QtSql.QSqlDatabase.addDatabase("QMYSQL3")
db.setDatabaseName("my_db")
db.setHostName("host")
db.setUserName("user")
db.setPassword("password")
db.open()
the open command returns False and the DB is not accessible. I call:
db.lastError()
and get:
# Result: <PySide2.QtSql.QSqlError("", "Driver not loaded", "Driver not loaded") at 0x0000000075410C08> #
NB: we successfully did the exact same thing with the previous PySide version.
Other threads mention similar things and it turned out to be missing the actual libmysql.dll, which is the actual MySql DLL and can result in the same error if missing. However I definitely have this on my system and it's definitely in the PATH. It's actually the same file that is being successfully picked up through PySide. But the PySide2 driver just wont open a connection to the DB.
I suspect maybe a dodgy driver but wondered if anyone has any experience with this or any advice?
I had the same error, but with postgres.
I solved by placing the libpq.dll connector library next to my script.py.
Then for mysql it would be libmysql.dll.
In my case I had to build the libpq.dll from the source of postgres 9.6
It seems that with PySide2 it is not necessary to use:
QtWidgets.QApplication.addLibraryPath ("C: \ path_to\ plugins")
using:
print (PySide2.QtCore.QLibraryInfo.location (PySide2.QtCore.QLibraryInfo.PluginsPath)), I could see that the path was correct.
a greeting

Perl error in MySQL and DBI

I'm a student of bioinformatics and I am using the OrthoMCL tool using this command
./orthomclLoadBlast my_orthomcl/orthomcl.config my_orthomcl/similarSequence.txt
but I am getting the error
Can't connect to data source ' :mysqllocalinfile=1' because I can't
work out what driver to use (it doesn't seem to contain a
'dbi:driver:' prefix and the DBI_DRIVER env var is not set) at
/home/linux/Desktop/orthomclSoftware-v2.0.9/bin/../lib/perl/OrthoMCLEngine/Main/Base.pm
line 56
DBI is already installed in my system, so why is it giving this error? How do I solve it?
You need database drivers to be installed for the DBI module to work with your database. In this case it looks like you need to install DBD::mysql.
It is possible that OrthoMCL omits the driver name and uses just dbi::, in which case you must set the environment variable DBI_DRIVER to mysql.
The proper answer must be in the documentation for OrthoMCL. Have you read it?
Update
Looking at the source for orthomclLoadBlast it looks like your config file is faulty. There is a chance that someone here is familiar with the utility, but you must publish orthomcl.config to stand a chance of getting any useful advice.
you should use mysql for the driver:
sample:
use DBI;
$dbh = DBI->connect('dbi:mysql:DBNAME','root','password')

xcode: Mysql connector library not work on iphone5 (armv7s) ,any solution?

I built my App using Mysql Connector/C to connect a remote Mysql database, its works fine on the simulator (no errors, no warnings) but when i try to run it on my device (iphone5) i got this error:
No architectures to compile for (ARCHS=armv7 armv7s, VALID_ARCHS=armv7 armv7s)
i tried -as in some answers- to change setting (Architectures - Build Active Architectures- Valid Architectures) but the error still, only when i change the setting (Architectures & Valid Architectures) to "armv6" its build without error but many warnings appears says:
warning: no rule to process file '(my App dir)/main.m' of type sourcecode.c.objc for architecture armv6
and also for all .m files, when i tried to start the App i got message:
Xcode cannot run using selected device
I know that the Connector library need to update , but are there any solution ?
your need compile the connector lib in xcode for iOS (armv6, armv7, armv7s, i386), then use lipo tool to combine output libs.
direct connect your mysql in app is not safe, a suggest way is setup a Apache+PHP+MySQL server, then on iPhone useing ASIHTTPRequest to connect your server.

propel could not find postgresql driver - wrong pdo version?

I can't get the propel script running on my system.
My build.properties looks like this:
propel.project = test
# The Propel driver to use for generating SQL, etc.
propel.database = pgsql
# This must be a PDO DSN
propel.database.url = pgsql:host=localhost;port=5432;dbname=test
propel.database.user = user
propel.database.password = password
I get the following error when I run propel-gen . reverse:
[propel-schema-reverse] There was an error building XML from metadata:
/home/user/web/dev/vendor/propel/generator/build-propel.xml:296:1:
could not find driver
pdo_pgsql is enabled (there are several applications running it succesfully); but in my phpinfo there seems to be a wrong version stated:
PostgreSQL(libpq) Version 8.4.13
I'm running PostgreSQL 9.1. I think this causes the error but I have no idea why the versions are different. I have installed first PostGreSQL and then php5-fpm etc. via apt-get.
Using Php 5.3.5 on Ubuntu 11.04. Can somebody help?
Remove all empty spaces and newlines in between the xml tags in the buildtime-conf.xml. That worked for me.
My file looked like this after using the runtime-conf.xml as a template for the buildtime-conf.xml
...
<adapter>
mysql
</adapter>
...
Now it looks like that and it works
...
<adapter>mysql</adapter>
...

Ruby on Rails MySQL #08S01Bad handshake - downgrade MySQL?

We recently upgraded from MySQL 5.1.41 to 5.1.61 on our Ubuntu 10.04LTS server. We have an ancient RoR web app that's now giving a bad handshake error:
Mysql::Error in MainController#index
#08S01Bad handshake
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:523:in `read'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/vendor/mysql.rb:153:in `real_connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:389:in `connect'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:152:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:82:in `mysql_connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:262:in `connection_without_query_cache='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/query_cache.rb:54:in `connection='
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:230:in `retrieve_connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/connection_specification.rb:78:in `connection'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:763:in `columns'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:2060:in `attributes_from_column_definition_without_lock'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/locking/optimistic.rb:45:in `attributes_from_column_definition'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1502:in `initialize_without_callbacks'
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:225:in `initialize'
#{RAILS_ROOT}/app/controllers/application.rb:48:in `new'
#{RAILS_ROOT}/app/controllers/application.rb:48:in `log_info'
/usr/local/bin/mongrel_rails:19:in `load'
/usr/local/bin/mongrel_rails:19
I googled around and stumbled onto http://bugs.ruby-lang.org/issues/5017 which tells me it's a Ruby MySQL extension bug. We aren't using the MySQL gem. Our web app is incredibly old and fragile (Ruby v1.8.7, Rails v1.2.3, Mongrel 1.1.5). We're in the process of replacing it with a Django rewrite, so we just need to get this functional for the next few weeks until we will replace it with the new site.
How can we get past this error? I'm thinking that downgrading to MySQL 5.1.41 is the best way to handle this, and then when we begin using the new site in a few weeks, we can re-upgrade to 5.1.61. However, I'm having an issue downgrading mysql. This is the command I'm using:
sudo aptitude install mysql-server-5.1=5.1.41-3ubuntu12.10
However, this tells me Unable to find a version "5.1.41-3ubuntu12.10" for the package "mysql-server-5.1". I've tried sudo aptitude install mysql-server-5.1=5.1.41 also, but that didn't work either. How can I have aptitude install the correct version of MySQL?
Instead of downgrade MySQL gem, it's possible to fix the database name parameter to fix the "bad handshake" problem.
I found this: https://github.com/rubygems/rubygems/issues/423 It's working good.
Instead of doing a hack in real_connect it's possible to add the "\0" in config/database.yml
production:
database: "itsalive_production\0"
adapter: mysql
host: localhost
encoding: UTF8
...
EDIT
If you use the solution with \0 at the end of the database name. You probably will find find out this and solve it yourself, but I mention it anyway:
(at least in my version of Rails)
Using the database string with \0 at the end gives problem when doing rake test. It starts with deleting the test database before copying the development database definitions, and then using a SQL command string that includes the test database name. This will cause an error because of the \0 in the middle of the string.
In my case I'm using a local development database that doesn't give any problem so I don't need to have \0 in that name.
Here is a alternative hack to solve that (original code in mysql_adapter.rb):
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
alias_method :old_execute, :execute
def execute(sql, name = nil) #:nodoc:
# This is needed because database names can end with "\0" to fix
# the issue with "handshake" when mysql server is newer than the gem
# requires. E.g. called when loading the new test db when doing "rake test".
sql = sql.delete("\0")
old_execute(sql, name)
end
end
end
end
I fixed it! Downgrading MySQL did the trick. We'll re-upgrade to 5.1.61 once the Django site goes live. Here's the command to downgrade MySQL:
sudo aptitude install mysql-server-5.1=5.1.41-3ubuntu12 mysql-client-5.1=5.1.41-3ubuntu12 mysql-server-core-5.1=5.1.41-3ubuntu12
I used apt-cache to get the exact version.
I also had the same issue. Please add:
config.gem 'mysql', :version => '2.7'
Then run rake gems:install.