# Read & Write data on a site

**URL:** <https://www.ruby-forum.com/t/read-write-data-on-a-site/185844>\
**Category:** Ruby\
**Created:** [March 27, 2010, 2:25am UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844 "2010-03-27T02:25:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![omegas7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@omegas7](https://www.ruby-forum.com/u/omegas7)\
**Post date:** [March 27, 2010, 2:25am UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844/1 "2010-03-27T02:25:33Z")

</div>

Hello. I got my own website and I would like it to have something like a  
database.

I want my application to be able to read, create or edit variables on  
the database.

Basically, how do I connect to a website and create variables there?  
Like, my application is an online game.

---

<div class="post-metadata">

**Author:** ![omegas7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@omegas7](https://www.ruby-forum.com/u/omegas7)\
**Post date:** [March 28, 2010, 2:02pm UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844/2 "2010-03-28T14:02:49Z")

</div>

Jose W. wrote:

> Hello. I got my own website and I would like it to have something like a  
> database.
> 
> I want my application to be able to read, create or edit variables on  
> the database.
> 
> Basically, how do I connect to a website and create variables there?  
> Like, my application is an online game.

Pick a programming language you are comfortable with - e.g. Ruby, PHP,  
Perl.

Pick a database you are comfortable with - e.g. Mysql, Postgres, Oracle

- and find out how to query it using your chosen programming language.  
(Ruby options include DBI for raw SQL, and  
ActiveRecord/Sequel/DataMapper/Og for a more object-oriented interface)

Then write programs which perform database queries and generate HTML  
output. Arrange so that these programs run in response to HTTP requests  
from your users (e.g. as CGI, using Phusion Passenger, using mod\_php  
…)

As you get more advanced, you may realise the need to separate your  
program logic from your HTML templates. A web framework may help you do  
this - e.g. Ruby on Rails, Sinatra, Django, etc.

Deploy these programs to your webspace - if your current web hosting  
doesn’t support it, you may need to move your website to a different  
provider.

If you don’t know where to start, probably your best bet is to buy a  
book with examples which you can run to guide you through the process.  
If you choose the Ruby + Rails approach I found “Agile Web D.  
with Rails” to be very good. There are tons of tutorials and screencasts  
you can watch too.

Note that Rails is not Ruby, it just happens to be written _in_ Ruby. If  
you have questions specifically about Rails, you’d best ask them on a  
Rails mailing list.

HTH,

Brian.

---

<div class="post-metadata">

**Author:** ![omegas7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@omegas7](https://www.ruby-forum.com/u/omegas7)\
**Post date:** [March 29, 2010, 8:00am UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844/3 "2010-03-29T08:00:04Z")

</div>

Brian C. wrote:

> Jose W. wrote:
> 
> > Hello. I got my own website and I would like it to have something like a  
> > database.
> > 
> > I want my application to be able to read, create or edit variables on  
> > the database.
> > 
> > Basically, how do I connect to a website and create variables there?  
> > Like, my application is an online game.
> 
> Pick a programming language you are comfortable with - e.g. Ruby, PHP,  
> Perl.
> 
> Pick a database you are comfortable with - e.g. Mysql, Postgres, Oracle
> 
> - and find out how to query it using your chosen programming language.  
> (Ruby options include DBI for raw SQL, and  
> ActiveRecord/Sequel/DataMapper/Og for a more object-oriented interface)
> 
> Then write programs which perform database queries and generate HTML  
> output. Arrange so that these programs run in response to HTTP requests  
> from your users (e.g. as CGI, using Phusion Passenger, using mod\_php  
> …)
> 
> As you get more advanced, you may realise the need to separate your  
> program logic from your HTML templates. A web framework may help you do  
> this - e.g. Ruby on Rails, Sinatra, Django, etc.
> 
> Deploy these programs to your webspace - if your current web hosting  
> doesn’t support it, you may need to move your website to a different  
> provider.
> 
> If you don’t know where to start, probably your best bet is to buy a  
> book with examples which you can run to guide you through the process.  
> If you choose the Ruby + Rails approach I found “Agile Web D.  
> with Rails” to be very good. There are tons of tutorials and screencasts  
> you can watch too.
> 
> Note that Rails is not Ruby, it just happens to be written _in_ Ruby. If  
> you have questions specifically about Rails, you’d best ask them on a  
> Rails mailing list.
> 
> HTH,
> 
> Brian.

Ok, so I will choose MySQL and Ruby. Is there a tutorial for MySQL  
queries using Ruby?

If all I want was just to have a simple place to read and write ruby  
variables, is it really necessary to have a web framework? Is there some  
kind of simple service that allows that? All I want is… Well, store  
data somewhere.

---

<div class="post-metadata">

**Author:** ![omegas7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@omegas7](https://www.ruby-forum.com/u/omegas7)\
**Post date:** [March 29, 2010, 12:27pm UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844/4 "2010-03-29T12:27:36Z")

</div>

Incidentally, there is a completely different approach: write the  
dynamic parts of your page as Javascript which runs inside the user’s  
web browser. It can make AJAX queries to a remote database to fetch  
data, and update the web page DOM with the results.

This is the model which couchdb implements: [http://couchdb.apache.org/](http://couchdb.apache.org/)

couchdb is essentially a database with a HTTP API. It can store and  
index JSON-structured documents, and arbitrary binary files as  
attachments (which means it can serve your static HTML and Javascript  
files too)

---

<div class="post-metadata">

**Author:** ![omegas7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/aca169/32.png) [@omegas7](https://www.ruby-forum.com/u/omegas7)\
**Post date:** [March 29, 2010, 10:27am UTC](https://www.ruby-forum.com/t/read-write-data-on-a-site/185844/5 "2010-03-29T10:27:42Z")

</div>

Jose W. wrote:

> Ok, so I will choose MySQL and Ruby. Is there a tutorial for MySQL  
> queries using Ruby?

Type “ruby mysql” into google. The fifth result is this:  
[http://www.kitebird.com/articles/ruby-mysql.html](http://www.kitebird.com/articles/ruby-mysql.html)

That’s writing code using the MySQL API directly.

If you prefer to go via a thin abstraction layer so that your code can  
talk to other databases too, try “ruby dbi”. I listed some  
object-oriented APIs earlier.

> If all I want was just to have a simple place to read and write ruby  
> variables, is it really necessary to have a web framework? Is there some  
> kind of simple service that allows that? All I want is… Well, store  
> data somewhere.

Well, think about how the web works.

1. A client clicks on a link, or clicks a submit button on a form
2. The client makes a HTTP GET or POST request to a web server
3. The web server generates some HTML and returns it to the client

What you need is a way for the generated HTML to depend on the contents  
of your database. Web frameworks give you an easy way to do this. For  
example with Sinatra you can write

get ‘/’ do # ← match incoming URL  
“hello world” # ← returned data  
end

Sinatra runs under Rack, a generic API for connecting ruby web servers  
to ruby applications. This means there are lots of ways to run your  
application without modifying the code - as a standalone server  
listening on a port, under Apache using Phusion Passenger, as a fastcgi  
etc.

I’d recommend this approach as both quick to get started and yet very  
flexible as you grow.

If you don’t want to do this for any reason, the main alternatives are:

1. CGI - the web server will start a ruby interpreter with your script  
when the incoming request arrives, setting information about the request  
in environment variables. Google “ruby cgi” for examples.

2. eruby/erb - you write a HTML page with ruby embedded directly in it,  
PHP-style. It’s possible to configure a web server to serve this  
directly.

CGI is inefficient because a new ruby interpreter is started for each  
incoming request. You can make it persistent using fastcgi or scgi.  
eruby/erb needs your web server specially configuring and can have  
problems with one page being able to pollute the environment used by  
another page.

HTH,

Brian.
