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'
Related
I have a 'customer' table and I am trying to get a record using Laravel Eloquent using the customer's id:
Customer::where('customer_id', '=', $customer_id)->get();
However, when this gets executed I check the MySQL logs and I get:
Prepare select `kanji_name` from `customer` where `customer_id` = ?
Close stmt
Quit
As you can see, there is no 'Execute ...' log which should be present of the query was executed after preparing it.
I also tried:
Customer::find($customer_id);
with the same result.
Does anyone know the reason why?
can you do this
$cust = Customer::where('customer_id', '=', $customer_id)->get();
dd($cust);
Sorry, not enugh reps to comment. What's DD output?
It seems there is some form of caching that is happening either on the Eloquent side or MySQL side (possibly on MySQL side).
I restarted my MySQL server and found that on my first try, I get
Prepare select * from `customer` where `customer_id` = ?
Execute select * from `customer` where `customer_id` = '4058'
Close stmt
Quit
While on succeeding tries, I get:
Prepare select * from `customer` where `customer_id` = ?
Close stmt
Quit
only.
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`"
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 am using Oracle (Still a little new to it) and each time i run the update statement below i get the following error message.
SQL Error: ORA-00904: "CH"."CONTRACT_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Here is the query:
UPDATE wr00262_catalogue_201310 ct SET
ct.PORTFOLIO_ID = (SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID)
WHERE ct.NPC in (SELECT ctl.NPC
FROM wr00262_contract_line ctl
WHERE ctl.CONTRACT_ID = ch.CONTRACT_ID);
I think i may need a join but not quite sure where or how. The contract_header table does have a column called CONTRACT_ID.
This is a scoping issue. The columns in one sub-query are not visible to any other sub-query. So try something like this:
UPDATE wr00262_catalogue_201310 ct SET
PORTFOLIO_ID = (
SELECT ch.PORTFOLIO_ID
from WR00262_CONTRACT_HEADER ch
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
)
WHERE ct.NPC in (
SELECT ctl.NPC
FROM WR00262_CONTRACT_HEADER ch
join wr00262_contract_line ctl
on ctl.CONTRACT_ID = ch.CONTRACT_ID
WHERE ch.PORTFOLIO_ID = ct.PORTFOLIO_ID
);
I'm trying to destroy an object using a call similar to
MyObject.destroy_all({:user_id => current_user.id, :item_type_id => params[:type_id], :item_id => params[:item_id]})
Rails generates this as a SQL commands:
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
MyObject Load (0.2ms) SELECT `my_objects`.* FROM `my_objects` WHERE `my_objects`.`user_id` = 1 AND `my_objects`.`item_type_id` = 3 AND `my_objects`.`item_id` = 9
(0.1ms) BEGIN
SQL (0.4ms) DELETE FROM `my_objects` WHERE `my_objects`.`` = NULL
The last SQL Statement causes this error (which makes sense)
Mysql2::Error: Unknown column 'my_objects.' in 'where clause': DELETE FROM `my_objects` WHERE `my_objects`.`` = NULL
Am I doing something wrong?
Rails 3.2.1
mysql2 0.3.11
mysql 5 (I think)