problems with ruby dbi

Reply

Join Date: Oct 2009
Posts: 1
Reputation: thegoatboy is an unknown quantity at this point 
Solved Threads: 0
thegoatboy thegoatboy is offline Offline
Newbie Poster

problems with ruby dbi

 
0
  #1
Oct 21st, 2009
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:

  1. #!/usr/bin/ruby
  2.  
  3. require 'dbi'
  4. require 'mysql'
  5.  
  6. # quit unless our script gets two command line arguments
  7. unless ARGV.length == 1
  8. puts "Wrong number of arguments\n"
  9. puts "Usage: ruby mysql-test*.rb csv_file\n"
  10. exit
  11. end
  12.  
  13. dbh = DBI.connect('DBI:Mysql:testlink', 'user', 'passwd')
  14.  
  15. query = dbh.prepare("SELECT field_id, node_id, value FROM cfield_design_values ORDER BY node_id;")
  16. query.execute()
  17. while row = query.fetch() do
  18. printf "field_id = %d, node_id = %d, value = %s \n", row[0], row[1], row[2]
  19. if ( row[0] == 7 )
  20. puts "we have a match"
  21. if row[2].empty?
  22. puts "Nothing to parse"
  23. else
  24. puts "Something to parse"
  25.  
  26. # Set up an empty array
  27. databaseArray = []
  28.  
  29. # Split the web address in the database into the array
  30. databaseArray = row[2].split("/")
  31.  
  32. # Check the end value
  33. $databaseSplit = databaseArray.last
  34. puts "value of the last element of the array = #$databaseSplit"
  35.  
  36. if $databaseSplit.include?("DispForm")
  37.  
  38. puts "Disp form found"
  39.  
  40. # set up an array for the string
  41.  
  42. $fileArray = []
  43. $fileHTTParray = []
  44.  
  45. # open the file for reading
  46. puts "opening the file"
  47.  
  48. # Create a bool for breaking when a match is found
  49. $matchFound = false
  50.  
  51. File.open(ARGV[0], "r") do |file|
  52. while line = file.gets
  53.  
  54. # Break from the loop when a match is found
  55. break(line) if $matchFound == true
  56.  
  57. #puts "matchFound in the first part of the loop is #$matchFound"
  58.  
  59. # Split the line of the file into an array
  60. $fileArray = line.split(",")
  61.  
  62. # Put into a new variable
  63. fileSplit = $fileArray[2]
  64.  
  65. # Split it into another array
  66. $fileHTTParray = fileSplit.split("/")
  67.  
  68. # put into a variable for comparison
  69. $fileCompare = $fileHTTParray.last
  70.  
  71. # Compare with the value from database
  72. #puts "#$databaseSplit, #$fileCompare"
  73.  
  74. if ( $databaseSplit == $fileCompare)
  75. puts "WE HAVE A MATCH !!!! \n"
  76.  
  77. printf "row[2] = %s\n", row[2]
  78. puts "filearray[4] = #$fileArray[4]\n"
  79.  
  80. row[2] = $fileArray[4]
  81.  
  82. printf "row[2] = %s\n", row[2]
  83. printf "row[0] = %d\n", row[0]
  84. printf "row[1] = %d\n", row[1]
  85.  
  86. $field_id = row[0]
  87. $node_id = row[1]
  88. $value = row[2]
  89.  
  90. # Change the value contained in the field node
  91. dbh.do("UPDATE cfield_design_values SET value=? WHERE field_id=? && node_id=?", $value, $field_node, $node_id)
  92. $matchFound = true
  93. else
  94. #puts "They do not match"
  95. $matchFound = false
  96. end
  97. end
  98. end
  99. else
  100. puts "Filename found"
  101. end
  102. end
  103. end
  104. end
  105. dbh.disconnect
Last edited by peter_budo; Oct 21st, 2009 at 11:41 am. Reason: Correcting code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
Nov 8th, 2009
use the word "AND" in your update SQL rather than &&
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Ruby Forum


Views: 858 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Ruby
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC