# Sql

**URL:** <https://www.ruby-forum.com/t/sql/168631>\
**Category:** Ruby\
**Created:** [May 23, 2009, 8:08am UTC](https://www.ruby-forum.com/t/sql/168631 "2009-05-23T08:08:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyhunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dfb087/32.png) [@rubyhunter](https://www.ruby-forum.com/u/rubyhunter)\
**Post date:** [May 23, 2009, 8:08am UTC](https://www.ruby-forum.com/t/sql/168631/1 "2009-05-23T08:08:37Z")

</div>

hi I have a text database which I looking for code.

my code is (Database.rb):

class Database

def initialize(input\_file)  
file = File.new(input\_file, “r”)  
while (line = file.gets)  
puts “#{parse(line)}”  
end  
file.close  
end

def parse(statement)  
return statement  
end

end

Database.new(“query.txt”)

running this incomplete code will just dump out the query.txt contents:

create table student regnum integer name string surname string  
create table results regnum integer courseCode string mark integer grade  
string  
create table bad regnum xxx courseCode string mark integer grade string  
create table notgood regnum1 integer name string surname string  
create table failure regnum integer name string surname  
insert into student 00001 ‘John’ ‘Ruby’  
insert into student 00002 ‘Sara’ ‘Java’

By completing the statement bit of the code it should output this:

create table student regnum integer name string surname string  
create table results regnum integer courseCode string mark integer grade  
string  
create table bad regnum xxx courseCode string mark integer grade string  
ERROR  
create table notgood regnum1 integer name string surname string  
ERROR  
create table failure regnum integer name string surname  
ERROR  
insert into student 00001 ‘John’ ‘Ruby’  
insert into student 00002 ‘Sara’ ‘Java’

Basically it checks the format by checking the “create table” or “insert  
into” commands and if the format is ok it will continue to next line and  
if incorrect it would print ERROR and check teh next line.

can anyone help me out with the code and tell me what should be there to  
in the following section and get it working for me:

def parse(statement)  
return statement  
end

many thanks

---

<div class="post-metadata">

**Author:** ![rubyhunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dfb087/32.png) [@rubyhunter](https://www.ruby-forum.com/u/rubyhunter)\
**Post date:** [May 24, 2009, 11:19am UTC](https://www.ruby-forum.com/t/sql/168631/2 "2009-05-24T11:19:03Z")

</div>

Ruby Hunter wrote:

> Basically it [will check] the format by checking the “create table”  
> or “insert into” commands and if the format is ok it will continue  
> to next line and if incorrect it would print ERROR and check teh  
> next line.

Why do you want to do that?

> can anyone help me out with the code and tell me what should  
> be there to in the following section and and get it working for me

You need to write an SQL parser. It isn’t diffrent than writing a parser  
for a programming language like pascal or C or whatever. It’s a science  
in its own.

But, again, why do you want to do that in the first place?

---

<div class="post-metadata">

**Author:** ![rubyhunter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dfb087/32.png) [@rubyhunter](https://www.ruby-forum.com/u/rubyhunter)\
**Post date:** [May 25, 2009, 6:17am UTC](https://www.ruby-forum.com/t/sql/168631/3 "2009-05-25T06:17:38Z")

</div>

Albert S. wrote:

> Ruby Hunter wrote:
> 
> > Basically it [will check] the format by checking the “create table”  
> > or “insert into” commands and if the format is ok it will continue  
> > to next line and if incorrect it would print ERROR and check teh  
> > next line.
> 
> Why do you want to do that?
> 
> > can anyone help me out with the code and tell me what should  
> > be there to in the following section and and get it working for me
> 
> You need to write an SQL parser. It isn’t diffrent than writing a parser  
> for a programming language like pascal or C or whatever. It’s a science  
> in its own.
> 
> But, again, why do you want to do that in the first place?

well its just part of my learning project as I am tryig to learn, so  
appreciate some script codes if you can help me. thanks
