Read & Write data on a site

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.

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.

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.

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/

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)

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs