I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into an array using the ActiveRecord database connection. I can't seem to figure out how to do this. can someone please enlighten me?
-C
Use what ActiveRecord gives you out of the box:
ActiveRecord::Base.connection.tables
I tried
ActiveRecord::Base.connection.execute("DESCRIBE TABLE table_name")
and was told to check my SQL manual. Doing so, I found that
ActiveRecord::Base.connection.execute("DESCRIBE table_name").each{|r| p r }
worked. Put whatever actual logic you need in the block.
Related
Please could someone help me understand why the following SQL doesn't work:
UPDATE `v2rns_content_new`
SET `images`= REPLACE(`images`, 'images\/news_images', 'images\/news_images\/legacy');
I am trying to find and replace the bold part of the following string (there are multiple records in the database with similar strings but the filenames e.g. example.png are different):
{"image_intro":"images\ /news_images\ /example.png","float_intro":"","image_intro_alt":"","image_intro_caption":"","image_fulltext":"","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}
Please note: I asked a related question yesterday (which will provide some background) but I feel that this question is best asked separately - Updating all rows within a single column using phpMyAdmin
I have managed to solve the issue by changing the already escaped \ to \
e.g. UPDATE v2rns_content_new SET images = REPLACE(images, 'images\\/news_images, 'images\\/news_images\/legacy')
Thanks to #GeolezTrol for pointing me in the right direction.
I'm new to Phing.
I'd like to query a value in a MySQL database table, and have the value set as a property so that I can echo it out nicely to the screen.
I can see that there is a PDOSQLExecTask which would allow me to run some SQL, but I can't see how to set the returned value into a property?
The query I want to run is:
SELECT MAX(change_number)
FROM changelog;
I'd like it set into a property:
Can anyone shed any light please?
Thanks,
Chris
I have access to MySQL at command line, I went with the following solution. I'm sure it's not the best, if someone else can improve it please do!
<!-- What's the latest delta that's been applied to this deployment? -->
<exec
command="${progs.mysql} -h${db.host} -u${db.user} -p${db.pass} -e 'USE ${db.main_db}; SELECT MAX(`change_number`) FROM `changelog`;'"
dir="."
checkreturn="false"
passthru="false"
outputProperty="latest_version_output"
/>
<php expression="preg_replace('/[^0-9]|\r|\n/si', '', '${latest_version_output}');" returnProperty="latest_version_applied" />
<echo msg="Latest delta applied was: ${latest_version_applied}" />
The PDOSQLExecTask comes with two default formatters, which will send their output to a file. To change this, you'd probably have to implement your own formatter. On the other hand, the task appears to read its SQL commands from a separate file with SQL commands, not the build file.
So on the whole, It seems to me like you might be better of writing your own task, probably using some code from the implementation of PDOSQLExecTask but with your own command input and result output. Unless calling the mysql command line binary is an alternative for you, in which case you could wrap up that call to redirect its output to a property using the outputProperty attribute to the ExecTask.
I'm a noob with sql and trying to figure my way through zencart. There's a bit in the code where it prepares a SELECT statement and amongst that there is the following line:
AND cd.language_id = :languagesID
I can't figure out what the : does. I thought maybe it refers to a $variable like $languagesID but I can't find that variable anywhere either.
What does the : do? Google left me none the wiser.
It's for prepared SQL statements. It's replaced later on with an actual value. Google "Prepared SQL"
I have more queries in mysql, but i not use the $this->query(), because I do not need to create arrays. in cakephp there any way? the function this->query force to go through the model.
thanks
You can try with DboSource query method
I'm trying to create a text file that contains the value of a custom field I added on redmine. I tried to get it from an SQL query in the create method of the project_controller.rb (at line 80 on redmine 1.2.0) as follows :
sql = Mysql.new('localhost','root','pass','bitnami_redmine')
rq = sql.query("SELECT value
FROM custom_values
INNER JOIN projects
ON custom_values.customized_id=projects.id
WHERE custom_values.custom_field_id=7
AND projects.name='#{#project.name}'")
rq.each_hash { |h|
File.open('pleasework.txt', 'w') { |myfile|
myfile.write(h['value'])
}
}
sql.close
This works fine if I test it in a separate file (with an existing project name instead of #project.name) so it may be a syntax issue but I can't find what it is. I'd also be glad to hear any other solution to get that value.
Thanks !
(there's a very similar post here but none of the solutions actually worked)
First, you could use Project.connection.query instead of your own Mysql instance. Second, I would try to log the SQL RAILS_DEFAULT_LOGGER.info "SELECT ..." and check if it's ok... And the third, I would use identifier instead of name.
I ended up simply using params["project"]["custom_field_values"]["x"] where x is the custom field's id. I still don't know why the sql query didn't work but well, this is much simpler and faster.