Ruby gem to run sql commands - sql

I want to run sql commands directly in my rails application. Is there any gem to do this?
For example:
I want to delete a user , who is having id 2 using below sql command
delete from users where id=2

ActiveRecord::Base.connection.execute("delete from users where id=2")

Yes. We can do that by using query_exec

If you want to run SQL from your browser try https://github.com/igorkasyanchuk/rails_db

Related

Laravel 5.4: logging SQL queries along with results

Logging SQL queries is widely described, for instance here:
How to get the query executed in Laravel 5?
but I found no infos about how to log the queries along with the query results or errors respectively.
Anyone who can fill the gap?
Thanks,
Armin.
IF you want to debug a query(ies) (based on your comment) there is this option
Before the query add
\DB::enableQueryLog();
and after the query you can do a dd or whatever with:
\DB::getQueryLog();
Note: This will debug all of the queries in between the two commands

How can I import a SQL file into a Rails database?

I've got a .sql file that I'd like to load into my Rails database using a Rake task. How can I do this?
The easiest way:
bundle exec rails db < $SQL_FILE
example:
bundle exec rails db < my_db.sql
The Easy Way
This works for simple cases.
ActiveRecord::Base.connection.execute(IO.read("path/to/file"))
Solution found on the Ruby On Rails mailing list from 2006 (but still works in 2011 on Rails 3.1).
Footnotes
This related question implied this solution, but rejected it for big imports. I wanted to show it explicitly, since it works for smaller ones.
The file I was trying to import contained a LOCK TABLES followed by an insert. The data was for a MySQL database. Mysql2 said it had an invalid SQL syntax error until I removed the lock and unlock statements.
On MySQL this gave me a syntax error. Splitting the sql into statements made it work.
sql = File.read(sql_file)
statements = sql.split(/;$/)
statements.pop # remove empty line
ActiveRecord::Base.transaction do
statements.each do |statement|
connection.execute(statement)
end
end

How to indicate in postgreSQL command in which database to execute a script? (simmilar to SQL Server "use" command)

I have the following problem, I need to put in a script that is going to run before the new version is rolled the SQL code that enables the pgAgent in PostgreSQL. However, this code should be run on the maintenance database (postgres) and the database where we run the script file is another one.
I remember that in SQL Server there is a command "use " so you could do something like:
use foo
-- some code
use bar
-- more code
is there something similar in PostgreSQL?
You can put in your file something like:
\c first_db_name
select * from t; --- your sql
\c second_db_name
select * from t; --- your sql
...
Are you piping these commands through the psql command? If so, \c databasename is what you want.
psql documentation
You can't switch databases in Postgres in this way. You actually have to reconnect to the other database.
PostgreSQL doesn't have the USE command. You would most likely use psql with the --dbname option to accomplish this, --dbname takes the database name as a parameter. See this link for details on the other options you can pass in you will also want to check out the --file option as well. http://www.postgresql.org/docs/9.0/interactive/app-psql.html
well after looking on the web for some time I found this which was what I need it
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html

Export MySQL Data as Insert Statements

I'm working in Ubuntu with MySql and I also have Query Browser and Administrator installed, I'm not afraid of the command line either if it helps.
I want simply to be able to run a query and see a result set but then convert that result set into a series of commands that could be used to create the same rows in a table of an identical schema.
I hope the question makes sense, it's quite a simple problem and one that must have been solved but I can't for the life of me work out where this kind of conversion is made available.
Thanks in advance,
Gav
I think you need to use a command line utility mysqldump http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
if you want to dump one or more tables.
If you need to dump a result of an arbitrary query and restore it later, take a look on SELECT ... INTO OUTFILE and LOAD DATA INFILE( http://dev.mysql.com/doc/refman/5.0/en/load-data.html)
I do not know if I understood you at all but you can use a SELECT INTO statement.
SELECT *
INTO new_table_name
FROM old_tablename
WHERE ...

Tracing SQL queries created by Ruby-on-Rails

In the web application I am currently developing, I have quite a few database queries being performed. I would like to know what parts of the code are producing these queries so that I can perhaps refactor the code to reduce them. Is there an easy way to do this?
Typically, the database queries are like:
SELECT count(*) AS count_all FROM 'stores' WHERE ('stores'.'distributor_id' = 1)
Thanks very much for your suggestions!
Gav
I think you're looking for QueryTrace.
To show the SQL of a query on a Ruby on Rails console just follow these steps:
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
path_to_project$ sc
Loading development environment (Rails 2.3.7)
>> User.first
User Load (0.8ms) SELECT * FROM users LIMIT 1