Skip to content

Instantly share code, notes, and snippets.

@googya
Created March 13, 2020 12:24
Show Gist options
  • Select an option

  • Save googya/fcd9d0bafb395d40ec283f12c2235d60 to your computer and use it in GitHub Desktop.

Select an option

Save googya/fcd9d0bafb395d40ec283f12c2235d60 to your computer and use it in GitHub Desktop.
require './mysql-connector-java-5.1.47-bin.jar'
require "rubygems"
require "java"
require "csv"
begin
# Prep the connection
Java::com.mysql.jdbc.Driver
userurl = "jdbc:mysql://10.52.3.224:3306/market"
connSelect = java.sql.DriverManager.get_connection(userurl, "username", "password(")
stmtSelect = connSelect.create_statement
# Define the query
selectquery = %q{select u.uid as uid, u.email as email, f.asset_code as coin, f.amount_available as avai, f.amount_lock as lk, f.amount_loan as lon from user as u join finance as f on f.uid = u.uid}
# Execute the query
rsS = stmtSelect.execute_query(selectquery)
# For each row returned do some stuff
res = []
while (rsS.next) do
row = []
row << rsS.getObject("uid")
row << rsS.getObject("email")
row << rsS.getObject("coin").to_s
row << rsS.getObject("avai").to_s
row << rsS.getObject("lk").to_s
row << rsS.getObject("lon").to_s
res.push(row)
end
File.write("/tmp/assets.csv", res.map(&:to_csv).join)
end
# Close off the connection
stmtSelect.close
connSelect.close
return true
# wget -c https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.48.tar.gz
# wget -c https://github.com/jruby/jruby/releases/download/9.2.11.0/jruby-dist-9.2.11.0-bin.tar.gz
@googya
Copy link
Copy Markdown
Author

googya commented Mar 13, 2020

jruby 有点好就是不需要安装, 开包即用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment