matkant_invoice/update.rb

30 lines
766 B
Ruby
Raw Normal View History

2017-01-31 14:38:08 +00:00
require 'mysql2'
load 'config.rb'
db = Mysql2::Client.new(:host => DB_HOST, :username => DB_USER, :database => DB_DB)
2017-01-30 13:06:38 +00:00
person = ARGV[0]
product = ARGV[1]
amount = ARGV[2]
log_dir = "./log/"
amount = 1.0 unless amount
2017-01-31 15:13:26 +00:00
valid_products = []
2017-02-01 11:37:43 +00:00
db.query("SELECT * FROM Products").each do |product|
2017-01-31 15:13:26 +00:00
valid_products << product['id']
end
unless valid_products.include? product
raise "Product not found in table\nValid products are: #{valid_products}"
end
2017-02-01 11:37:43 +00:00
statement = db.prepare("INSERT INTO Transactions (buyer, product, amount) VALUES (?, ?, ?)")
2017-01-31 14:38:08 +00:00
result = statement.execute(person,product,amount)
puts result
#output = File.open("#{log_dir}/matkant-#{Time.now.strftime('%Y%W')}.log","a+")
#output << "#{Time.now.to_i};#{person};#{product};#{amount}\n"
#output.close