| | |
problems with ruby dbi
Please support our Ruby advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2009
Posts: 1
Reputation:
Solved Threads: 0
Hello
Hopefully someone can help me.
I am trying to update fields in a mysql database using ruby with dbi, but no matter what I try, i just cannot update the row.
I am able to search it, but never update.
I search for the right string, then open a file, search for that string and then update the mysql row. but the update part will not work.
here is the code:
Hopefully someone can help me.
I am trying to update fields in a mysql database using ruby with dbi, but no matter what I try, i just cannot update the row.
I am able to search it, but never update.
I search for the right string, then open a file, search for that string and then update the mysql row. but the update part will not work.
here is the code:
Ruby Syntax (Toggle Plain Text)
#!/usr/bin/ruby require 'dbi' require 'mysql' # 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:testlink', '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 of the file into an array $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 dbh.do("UPDATE cfield_design_values SET value=? WHERE field_id=? && node_id=?", $value, $field_node, $node_id) $matchFound = true else #puts "They do not match" $matchFound = false end end end else puts "Filename found" end end end end dbh.disconnect
Last edited by peter_budo; Oct 21st, 2009 at 11:41 am. Reason: Correcting code tags
![]() |
Similar Threads
- Problems running Jruby with Glimmer (Ruby)
- News Story: It’s Like AJAX for Ruby and Python (Ruby)
- Ruby on Rails developer (Web Development Job Offers)
- Senior Ruby On Rails Developer (VIRTUAL) (Software Development Job Offers)
- having trouble executing ruby coding (Ruby)
- Ruby on Rails Web Developer (Web Development Job Offers)
- Ruby on Rails category (DaniWeb Community Feedback)
- Learning Ruby (Ruby)
Other Threads in the Ruby Forum
- Previous Thread: this regular expression
- Next Thread: Mixing PHP and Ruby together... opinions and suggestions.
| Thread Tools | Search this Thread |





