Sponsored Content
Special Forums UNIX and Linux Applications ruby/SQLite database interface Post 302663291 by LMHmedchem on Thursday 28th of June 2012 12:58:28 AM
Old 06-28-2012
ruby/SQLite database interface

Hello,

I'm not sure this is quite the right place, but there do seem to be allot of posts with folks using ruby to play nicely with databases so I thought I would give it a go.

I am starting a long process of developing a database application bases on SQLite and ruby. This will run on various linux flavors as well as windows/cygwin. I am using ruby because the interface will eventually be browser based and ruby on rails or Sinatra seems like a good way to get that working. I have looked at some tutorials, but they are either too simple or too complicated. My first step is to do a simple import of a tab delimited text file to create a SQLlite database. There would be one database and several tables, so I need to know how to map each column in the file to table/column. It could be just one table for now if that is simpler.

Can some point me in the right direction to get me going? I have worked through allot of this tutorial,
SQLite Ruby tutorial

This code,
Code:
#!/usr/bin/ruby

require 'sqlite3'

begin
    
    db = SQLite3::Database.open "test.db"
    db.execute "CREATE TABLE IF NOT EXISTS Cars(Id INTEGER PRIMARY KEY, 
        Name TEXT, Price INT)"
    db.execute "INSERT INTO Cars VALUES(1,'Audi',52642)"
    db.execute "INSERT INTO Cars VALUES(2,'Mercedes',57127)"
    db.execute "INSERT INTO Cars VALUES(3,'Skoda',9000)"
    db.execute "INSERT INTO Cars VALUES(4,'Volvo',29000)"
    db.execute "INSERT INTO Cars VALUES(5,'Bentley',350000)"
    db.execute "INSERT INTO Cars VALUES(6,'Citroen',21000)"
    db.execute "INSERT INTO Cars VALUES(7,'Hummer',41400)"
    db.execute "INSERT INTO Cars VALUES(8,'Volkswagen',21600)"
    
rescue SQLite3::Exception => e 
    
    puts "Exception occured"
    puts e
    
ensure
    db.close if db
end

inserts some data to a table, but a hard coded comma separated list is a long way from a delimited text file. I think it's fine for the mapping to be hard coded for now, but this is something that would eventually be entered from a browser interface. At the end of the day, one function would be to select a file from a web interface and load it into a SQLite database. There would be mapping for each column in the file to a table/column in the database, and also a hash to determine if the record already exists. Where a record already exists, data could be added, where it doesn't exist, it will be created.

This is relatively straightforward stuff, but it always seems to take a while to get going.

I can post some sample files if that would help.

Thanks for the advice,

LMHmedchem
 

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sqlite issue?

i'm on freebsd 5.2.1, and from a fresh installation i've used pkg_add for the latest ported version of apache, as well as installing php 5. supposedly php5 comes with native support for sqlite (in the binary package), and this is what i added. i am trying to install a site engine (the 'gyrator'... (0 Replies)
Discussion started by: brandan
0 Replies

2. HP-UX

sqlite database in HP-UX vs Linux

Hi everybody, We have a cgi application which accesses sqlite database. It works fine in Linux environment but the same code doesn't enter data into the database when done in HP-UX environment. Should the codes vary depending on whether it is Linux or HP-UX. Regards Ruma (1 Reply)
Discussion started by: perlprg
1 Replies

3. Shell Programming and Scripting

hell and sqlite

Hi everyone, I have a requirement that requires me to fill an sqlite database with 100,000 entries (no duplicates). I will start out by giving the command that will insert the values necessary to populate the database: # sqlite /var/local/database/dblist "insert into list... (2 Replies)
Discussion started by: ogoy
2 Replies

4. SCO

Change SCO - GUI or Desktop interface to DOS based interface

Hi all I have installed a demo version of SCO OpenServer 5.0.2, I finally found it is Desktop Interface, I would like to know how to change its interface to dos based interface? If you have any ideas, please tell me then. Thank you (2 Replies)
Discussion started by: TinhNhi
2 Replies

5. Shell Programming and Scripting

Several processes writing to an SQLite database at the same time

I have several bash scripts that write to an SQLite3 database at the same time. At some occasion the database returns: SQL error: database is locked. How would be the best way, to make a process to wait until the data base is 'free' again. I tried: sqlite3 test.db ".timeout 1000; update....."... (2 Replies)
Discussion started by: creamcheese
2 Replies

6. Web Development

Web Interface for a database

Hi Gents, What is the best way to create a web interface for mysql database? That I can manage my data for root user and for normal user as well according to their privileges? Assuming that I use Linux as a server and Apache as a web server and my database is mysql. Thanks in advance (5 Replies)
Discussion started by: leo_ultra_leo
5 Replies

7. AIX

Need a graphical interface on AIX server to create database

Hello, Please suggest me the ways how to get graphical interface on AIX server.I need to create oracle database for which I need graphical access. Best regards, Vishal (4 Replies)
Discussion started by: Vishal_dba
4 Replies
INSERT(7)							   SQL Commands 							 INSERT(7)

NAME
INSERT - create new rows in a table SYNOPSIS
INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query } [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ] DESCRIPTION
INSERT inserts new rows into a table. One can insert one or more rows specified by value expressions, or zero or more rows resulting from a query. The target column names can be listed in any order. If no list of column names is given at all, the default is all the columns of the table in their declared order; or the first N column names, if there are only N columns supplied by the VALUES clause or query. The values sup- plied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right. Each column not present in the explicit or implicit column list will be filled with a default value, either its declared default value or null if there is none. If the expression for any column is not of the correct data type, automatic type conversion will be attempted. The optional RETURNING clause causes INSERT to compute and return value(s) based on each row actually inserted. This is primarily useful for obtaining values that were supplied by defaults, such as a serial sequence number. However, any expression using the table's columns is allowed. The syntax of the RETURNING list is identical to that of the output list of SELECT. You must have INSERT privilege on a table in order to insert into it. If a column list is specified, you only need INSERT privilege on the listed columns. Use of the RETURNING clause requires SELECT privilege on all columns mentioned in RETURNING. If you use the query clause to insert rows from a query, you of course need to have SELECT privilege on any table or column used in the query. PARAMETERS
table The name (optionally schema-qualified) of an existing table. column The name of a column in table. The column name can be qualified with a subfield name or array subscript, if needed. (Inserting into only some fields of a composite column leaves the other fields null.) DEFAULT VALUES All columns will be filled with their default values. expression An expression or value to assign to the corresponding column. DEFAULT The corresponding column will be filled with its default value. query A query (SELECT statement) that supplies the rows to be inserted. Refer to the SELECT [select(7)] statement for a description of the syntax. output_expression An expression to be computed and returned by the INSERT command after each row is inserted. The expression can use any column names of the table. Write * to return all columns of the inserted row(s). output_name A name to use for a returned column. OUTPUTS
On successful completion, an INSERT command returns a command tag of the form INSERT oid count The count is the number of rows inserted. If count is exactly one, and the target table has OIDs, then oid is the OID assigned to the inserted row. Otherwise oid is zero. If the INSERT command contains a RETURNING clause, the result will be similar to that of a SELECT statement containing the columns and val- ues defined in the RETURNING list, computed over the row(s) inserted by the command. EXAMPLES
Insert a single row into table films: INSERT INTO films VALUES ('UA502', 'Bananas', 105, '1971-07-13', 'Comedy', '82 minutes'); In this example, the len column is omitted and therefore it will have the default value: INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, '1961-06-16', 'Drama'); This example uses the DEFAULT clause for the date columns rather than specifying a value: INSERT INTO films VALUES ('UA502', 'Bananas', 105, DEFAULT, 'Comedy', '82 minutes'); INSERT INTO films (code, title, did, date_prod, kind) VALUES ('T_601', 'Yojimbo', 106, DEFAULT, 'Drama'); To insert a row consisting entirely of default values: INSERT INTO films DEFAULT VALUES; To insert multiple rows using the multirow VALUES syntax: INSERT INTO films (code, title, did, date_prod, kind) VALUES ('B6717', 'Tampopo', 110, '1985-02-10', 'Comedy'), ('HG120', 'The Dinner Game', 140, DEFAULT, 'Comedy'); This example inserts some rows into table films from a table tmp_films with the same column layout as films: INSERT INTO films SELECT * FROM tmp_films WHERE date_prod < '2004-05-07'; This example inserts into array columns: -- Create an empty 3x3 gameboard for noughts-and-crosses INSERT INTO tictactoe (game, board[1:3][1:3]) VALUES (1, '{{" "," "," "},{" "," "," "},{" "," "," "}}'); -- The subscripts in the above example aren't really needed INSERT INTO tictactoe (game, board) VALUES (2, '{{X," "," "},{" ",O," "},{" ",X," "}}'); Insert a single row into table distributors, returning the sequence number generated by the DEFAULT clause: INSERT INTO distributors (did, dname) VALUES (DEFAULT, 'XYZ Widgets') RETURNING did; COMPATIBILITY
INSERT conforms to the SQL standard, except that the RETURNING clause is a PostgreSQL extension. Also, the case in which a column name list is omitted, but not all the columns are filled from the VALUES clause or query, is disallowed by the standard. Possible limitations of the query clause are documented under SELECT [select(7)]. SQL - Language Statements 2010-05-14 INSERT(7)
All times are GMT -4. The time now is 12:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy