943,962 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Unsolved
  • Views: 1924
  • Ruby RSS
Oct 21st, 2009
0

problems with ruby dbi

Expand Post »
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:

Ruby Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
thegoatboy is offline Offline
1 posts
since Oct 2009
Nov 8th, 2009
0
Re: problems with ruby dbi
use the word "AND" in your update SQL rather than &&
Featured Poster
Reputation Points: 290
Solved Threads: 277
Posting Virtuoso
thines01 is offline Offline
1,688 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: this regular expression
Next Thread in Ruby Forum Timeline: Mixing PHP and Ruby together... opinions and suggestions.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC