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`"
Related
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.
I connected to postgres database from ruby with no problem, but when they added schema , I got confused and go error , here is the code I am trying to run :
require 'pg'
#pg_conn = PGconn.connect("xxxxxxx.us-gov-west-1.rds.amazonaws.com", 5432, '', '', "BRCArchive", "yyyy", "zzzz")
count = #pg_conn.exec('SELECT COUNT(*) FROM "brcmanager.Agency"')
puts count
I got this error :
Called from brc_migration2.rb:7:in `<main>'
brc_migration2.rb:9:in `exec': ERROR: relation "brcmanager.Agency" does not exist (PG::UndefinedTable)
LINE 1: SELECT COUNT(*) FROM "brcmanager.Agency"
^
from brc_migration2.rb:9:in `<main>'
Thanks,
#pg_conn.exec("set search_path=brcmanager;")
and then
count = #pg_conn.exec('SELECT COUNT(*) FROM Agency')
The error tells you what's wrong. The table brcmanager.Agency does not exist in that database, perhaps you misspelled it?
You can see what tables exist in that database by running this SQL:
SELECT * FROM information_schema.tables WHERE table_schema = 'information_schema'
In JDBC To Other Databases I found the following explanation of dbtable parameter:
The JDBC table that should be read. Note that anything that is valid in a FROM clause of a SQL query can be used. For example, instead of a full table you could also use a subquery in parentheses.
When I use the code:
CREATE TEMPORARY TABLE jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "mytable"
)
everything works great, but the following:
dbtable "SELECT * FROM mytable"
leads to the error:
What is wrong?
Since dbtable is used as a source for the SELECT statement it has be in a form which would be valid for normal SQL query. If you want to use subquery you should pass a query in parentheses and provide an alias:
CREATE TEMPORARY TABLE jdbcTable
USING org.apache.spark.sql.jdbc
OPTIONS (
url "jdbc:postgresql:dbserver",
dbtable "(SELECT * FROM mytable) tmp"
);
It will be passed to the database as:
SELECT * FROM (SELECT * FROM mytable) tmp WHERE 1=0
Code In Scala
val checkQuery = "(SELECT * FROM " + inputTableName + " ORDER BY " + columnName + " DESC LIMIT 1) AS timetable"
val timeStampDf = spark.read.format("jdbc").option("url", url).option("dbtable", checkQuery).load()
Adding an alias is also necessary after the query in parenthesis.
Thanks for your time!
I use OCI8 to connect Oracle and execute some sql statement.
But when I do as following:
conn = OCI8.new('ci/a#localhost/orcl')
cursor = conn.parse('desc table_name')
cursor.exec
It raised the error:
OCIError: ORA-00900: invalid SQL statement
I've asked some DBA and they told me that desc is Data Definition Language (DDL) , which is not a normal SQL, and probably that's what caused the issue.
I use Ruby as my scripting language. How could I solve this problem?
desc is not DDL. It is a sqlplus command.
Use OCI8#describe_table or dictionary views as follows:
conn.describe_table('TABLE_NAME').columns.each do |col|
puts format('%-30s %s', col.name, col.data_type_string)
end
or
conn.exec("select column_name, data_type
from all_tab_columns
where owner = 'OWNER_NAME'
and table_name = 'TABLE_NAME'
order by column_id") do |row|
puts format('%-30s %s', row[0], row[1])
end
The former works for tables, views and synonyms. The latter works only for tables.
I have a an sqlite database with the table test. Several processes are accessing this database from bash. The table has the following fields:
CREATE TABLE mytable (id NUMERIC,
start JULIAN,
finish JULIAN)
I obtain an unique id by:
id=$(sqlite test.db <<EOF
BEGIN EXCLUSIVE;
SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1;
COMMIT;
EOF
)
My question is, how can update the field start with:
UPDATE mytable set start=julianday('now') where id="SELECTED ID FROM ABOVE";
In the same statement?
Based on the comments that you supplied above, my solution would look something like follows (in perl with a raw DBI connection, also i didn't do a lot of error checking or anything either, something that you should probably do):
my $dbh = DBI->connect(...);
$dbh->do("BEGIN EXCLUSIVE");
my $stm = $dbh->prepare("SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1");
$stm->execute();
my $row = $stm->fetchrow_hashref();
my $id = undef;
if ( $row ) {
$id = $row->{ID};
my $ustm = $dbh->prepare("UPDATE mytable set start=julianday('now') where id=?");
$ustm->execute($id);
}
$dbh->do("COMMIT");
# Still have the id at this point.