Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > mysql update with ruby + dbi

Reply
Thread Tools

mysql update with ruby + dbi

 
 
Steven Price
Guest
Posts: n/a
 
      10-21-2009
Hello.

Hopefully someone can help me and stop me form smashing my skull to bits
on the desk...

I am trying to modify a mysql database using a file for information.

I can read the database fine, and parse it, but it just won't update the
column as I want.

I have tried about 2000 different ways, but always get the same result.
The information I want to change just doesnt update.

The code is posted below. I know there are a lot of exchanges of data,
but I have only really been using ruby for about 2 days. So don't shout
at me for the coding

Here is my code:

Code:
#!/usr/bin/ruby

require 'dbi'

# quit unless our script gets two command line arguments
unless ARGV.length == 1
puts "Wrong number of arguments\n"
puts "Usage: ruby mysql-test*.rb csv_file\n"
exit
end

dbh = DBI.connect('DBI:Mysql:database', 'user', 'passwd')

query = dbh.prepare("SELECT field_id, node_id, value FROM
cfield_design_values ORDER BY node_id;")
query.execute()
while row = query.fetch() do
printf "field_id = %d, node_id = %d, value = %s \n", row[0], row[1],
row[2]
if ( row[0] == 7 )
puts "we have a match"
if row[2].empty?
puts "Nothing to parse"
else
puts "Something to parse"

# Set up an empty array
databaseArray = []

# Split the web address in the database into the array
databaseArray = row[2].split("/")

# Check the end value
$databaseSplit = databaseArray.last
puts "value of the last element of the array = #$databaseSplit"

if $databaseSplit.include?("DispForm")
puts "Disp form found"

# set up an array for the string

$fileArray = []
$fileHTTParray = []

# open the file for reading
puts "opening the file"

# Create a bool for breaking when a match is found
$matchFound = false

File.open(ARGV[0], "r") do |file|
while line = file.gets

# Break from the loop when a match is found
break(line) if $matchFound == true

#puts "matchFound in the first part of the loop is #$matchFound"

# Split the line

$fileArray = line.split(",")

# Put into a new variable
fileSplit = $fileArray[2]

# Split it into another array
$fileHTTParray = fileSplit.split("/")

# put into a variable for comparison
$fileCompare = $fileHTTParray.last

# Compare with the value from database
#puts "#$databaseSplit, #$fileCompare"

if ( $databaseSplit == $fileCompare)
puts "WE HAVE A MATCH !!!! \n"

printf "row[2] = %s\n", row[2]
puts "filearray[4] = #$fileArray[4]\n"

row[2] = $fileArray[4]

printf "row[2] = %s\n", row[2]
printf "row[0] = %d\n", row[0]
printf "row[1] = %d\n", row[1]

$field_id = row[0]
$node_id = row[1]
$value = row[2]

# Change the value contained in the field node
sql = ("UPDATE cfield_design_values SET value='$value' WHERE
field_id='$field_id' && node_id='$node_id'")
dbh.prepare(sql)
dbh.execute(sql)
dbh.commit

$matchFound = true

else

#puts "They do not match"
$matchFound = false

end
end
end
else
puts "Filename found"
end
end
end
end
dbh.disconnect
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Charles Johnson
Guest
Posts: n/a
 
      10-21-2009
On Oct 21, 2009, at 3:58 AM, Steven Price wrote:

> Hello.
>
> Hopefully someone can help me and stop me form smashing my skull to
> bits
> on the desk...
>
> I am trying to modify a mysql database using a file for information.
>
> I can read the database fine, and parse it, but it just won't update
> the
> column as I want.
>

Any error messages? Relevant log entries? You have the relevant write
permissions to the database? A bit more information would be helpful.

Cheers--

Charles
--
Charles Johnson
Advanced Computing Center for Research and Education
Office: 615-343-2776
Cell: 615-478-5743






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Update statement with Perl DBI for MySQL 5.0 sam Perl Misc 2 01-18-2005 06:27 PM
DBI Mysql storing DBI:binary, trouble with character 26 Jerome Hauss Ruby 0 10-13-2004 03:04 PM
mysql-ruby or ruby-mysql? Randy Lawrence Ruby 3 05-24-2004 03:30 PM
dbi:mysql mysql has gone away Paul Vudmaska Ruby 0 04-21-2004 06:19 PM
Self compiled ruby (cygwin) + ruby-dbi + mysql...stuck. Michael campbell Ruby 0 10-22-2003 04:07 AM



Advertisments