Jruby code to truncate a table with snowflake - ruby

This is the code I am using to connect to the snowflake database:
require 'java'
require 'rubygems'
require 'C:/Program Files/CData/CData JDBC Driver for Snowflake 2018/lib/cdata.jdbc.snowflake.jar'
url = "jdbc:snowflake:User=Admin;Password=test123;Server=localhost;Database=Northwind;Warehouse=TestWarehouse;Account=Tester1;"
conn = java.sql.DriverManager.getConnection(url)
stmt = conn.createStatement
rs = stmt.executeQuery("SELECT Id, ProductName FROM Products")
while (rs.next) do
puts rs.getString(1) + ' ' + rs.getString(2)
end
But I need to run other SQL queries as well:
sql_query1: Copy into table1 from table2
sql_query2: Truncate table student
How can I run these queries using my code?

I assume you would just update your sql
rs = stmt.executeQuery("SELECT Id, ProductName FROM Products")
Change that sql to something other than the example you found.

Related

Oracle OJDBC MERGE Statement and Generated Keys

Does Oracle ~>12 support generated keys using a Merge statement? Some sudo code..
MERGE INTO TARGET_TABLE TRG
USING (SELECT CAST(? AS NUMBER) AS ID FROM DUAL) SRC
ON (TRG.ID = SRC.ID)
WHEN MATCHED THEN UPDATE SET....
WHEN NOT MATCHED THEN
INSERT(ID....)
VALUES(MYSEQ.NEXTVAL...)
The prepared statement is set up;
try (PreparedStatement pstmt =
connection.prepareStatement(
loadStatement(sqlName, connection, getClass()), new String[] {ID})) {
...
int inserted = pstmt.executeUpdate();
ResultSet rs = pstmt.getGeneratedKeys();
List<Long> keys = new ArrayList<>(inserted);
while (rs.next) {
rs.getLong(1);
}
return keys;
...
I have in-memory tests where the connection uses the H2 driver running the very same SQL and PreparedStatment and that returns the generated key just fine, but Oracle does not.
Reviewing the docs it would suggest it does not?
Thanks!

hive jdbc on java get count(*) of table rows

i am trying to get rows count of my table in my java project using jdbc connection to Hive database.
My code:
Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM TABLE_NAME");
r.next();
int count = r.getInt("rowcount") ;
r.close() ;
System.out.println("MyTable has " + count + " row(s).");
When i start this function, my project freeze and nothing happens. I tried to debug, the result was: when program reach this place
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM TABLE_NAME");
nothing happens and debuger shut down.
select * from TABLE_NAME
works fine, but counting rows from the same table doesnt work. Any ideas? :)

Query results in Oracle SQL Developer are different to those using .Net ODP

I have this query:
SELECT
case
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Postal%')
then 'PostalAddress'
when
AddressType IN (select code.codeid from code where code.codeset = AddressTypeCodeSet and code.description like 'Email%')
then
'EmailAddress'
else
'OtherAddress'
end as AddressType
FROM VW_Address
WHERE AddressId=190000;
When I execute this query using SQL Developer, the result returned is OtherAddress. This is what I'm expecting. However, when I execute the same query via this code:
Dim cmd =
New Oracle.DataAccess.Client.OracleCommand(["SQL HERE]")
cmd.Parameters.Add(":p0", OracleDbType.Int32)
cmd.Parameters(0).Value = 190000
cmd.BindByName = True
Dim r = cmd.ExecuteReader()
I get a different result: EmailAddress
ODP is version 2.111.7.20, Oracle is 11g.

Dropping tables in sqlite3 in ruby

I am trying to drop a table from a database. However I keep getting an error. The code and the error message are below. Appreciate your help. The table csv_07-15-2014_10-00 is present in the DB.
require 'win32ole'
require 'sqlite3'
DB_NAME = 'excel.db'
db = SQLite3::Database.new(DB_NAME)
sqlQuery = "SELECT * FROM sqlite_master WHERE type = 'table'"
puts db.execute(sqlQuery )
sqlQuery = "DROP TABLE csv_07-15-2014_10-00"
puts sqlQuery
puts db.execute(sqlQuery)
Error message is below:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/sqlite3/databa
se.rb:91:in `initialize': near "-": syntax error (SQLite3::SQLException)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:91:in `new'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:91:in `prepare'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.9-x86-mingw32/lib/s
qlite3/database.rb:134:in `execute'
from sqldroptable.rb:19:in `<main>'
PUT Statement output is below:
table
csv_07-15-2014_10-00
csv_07-15-2014_10-00
2
CREATE TABLE [csv_07-15-2014_10-00]
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )
table
csv_07-14-2014_22-30
csv_07-14-2014_22-30
8
CREATE TABLE [csv_07-14-2014_22-30]
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )
table
csv_07-14-2014_22-29
csv_07-14-2014_22-29
9
CREATE TABLE [csv_07-14-2014_22-29]
( mineId,prod_year,prod_qtr,subunit_cd,subunit,qtr_hrs,coal_prod,avg_emp_cnt )
DROP TABLE csv_07-15-2014_10-00 FROM sqlite_maste
r
You have SQL exception i think problem in escaping query try this:
sqlQuery = "DROP TABLE \"csv_07-15-2014_10-00\""
In SQL, - is the subtraction operator, which does not make sense in a DROP TABLE statement.
If you have special characters in an identifier, you must quote it:
sqlQuery = "DROP TABLE [csv_07-15-2014_10-00]"
or
sqlQuery = "DROP TABLE \"csv_07-15-2014_10-00\""
or
sqlQuery = "DROP TABLE `csv_07-15-2014_10-00`"

What is the best way to parameterize a LIKE query?

I'm trying to implement a simple search, using LIKE in my SQL statement:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like '%:text%'"
cmd.Parameters.AddWithValue("text", searchValue)
...
End Using
This doesn't work - what is the best way to parameterize the search value?
select * from TABLE where FIELD like ('%' || :text || '%')
update: my bad, if you are using oracle driver, see this link for help. this depends on which driver you use for db access, in case of oledb you need to do following
here is corrected code:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like ?"
cmd.Parameters.AddWithValue("#text", '%'+searchValue+'%')
...
End Using
in case of oracle driver you need to do following:
Using cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from TABLE where FIELD like :text"
cmd.Parameters.AddWithValue(":text", '%'+searchValue+'%')
...
End Using
In cases like this I prefer this syntax:
select * from TABLE where INSTR(FIELD, :text) > 0

Resources