Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting Ruby to Mysql without rails

Tags:

ruby

How can i connect Ruby to Mysql without rails? I want to use Ruby standalone to write pure ruby code to make web application. with no abstraction

like image 505
RubyBeginner Avatar asked Aug 01 '12 07:08

RubyBeginner


People also ask

How to connect to the MySQL database using Ruby?

Launch a new command prompt (cmd) from the Start menu. Test the Ruby installation by running the command ruby -v to see the version installed. Test the Gem installation by running the command gem -v to see the version installed. Build the Mysql2 module for Ruby using Gem by running the command gem install mysql2 .

What is Mysql2?

MySQL2 project is a continuation of MySQL-Native. Protocol parser code was rewritten from scratch and api changed to match popular mysqljs/mysql. MySQL2 team is working together with mysqljs/mysql team to factor out shared code and move it under mysqljs organisation.

Does Ruby on Rails work with MySQL?

Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL.


2 Answers

Look here

require "mysql"    # if needed

@db_host  = "localhost"
@db_user  = "root"
@db_pass  = "root"
@db_name = "your_db_name"

client = Mysql::Client.new(:host => @db_host, :username => @db_user, :password => @db_pass, :database => @db_name)
@cdr_result = client.query("SELECT * from your_db_table_name')
like image 85
Kashiftufail Avatar answered Oct 21 '22 07:10

Kashiftufail


install gem mysql (check that you have libmysqlclient-dev installed).

mysql = Mysql.new(host, user, password, database)
mysql.query("SELECT ...")
mysql.close

See the documentation for more information

like image 43
Vodun Avatar answered Oct 21 '22 07:10

Vodun