Ruby Mysql - load .sql file and execute - mysql

I'm trying to load a .sql file in a ruby script into a string and then execute it.
For some reason i'm getting a Syntax error, however if i copy and paste the statements directly into mysql it works just fine.
Here's how i do it:
text = File.read(src_sql_file)
new_text = text.DOINGSOMEGSUBSTUFF
#dbh.select_db(dbname)
sql = #dbh.prepare(new_text)
sql.execute()
i've also tried this:
sql = #dbh.prepare(new_text) do |sth|
sth.execute()
end
and i always get:
Error message: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PR' at line 5
where the sql for this looks like that:
CREATE TABLE `the_logs` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`timestamp` INT( 11 ) NOT NULL ,
...
Any idea if i'm doing this wrong using prepare? i've also tried query.. didn't work either.
Any help is greatly appreciated.
T

I expect whichever library you're using to talk to MySQL wants to handle one statement at a time and you're passing it several statements at once.
Try splitting the string on semicolons and executing each statement individually.

Related

Creating a database in MariaDB-10.2.9 using the schema that comes with Barnyard2

I used phpMyAdmin to create the tables in the Snort database but the creation of two of the tables fails with the seemingly ubiquitous error 1064. The schema entries are as follows -
CREATE TABLE reference(ref_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ref_system_id INT UNSIGNED NOT NULL,
ref_tag TEXT NOT NULL,
PRIMARY KEY(ref_id));
CREATE TABLE reference_system(ref_system_id INT UNSIGNED NOT NULL
AUTO_INCREMENT,
ref_system_name VARCHAR(20),
PRIMARY KEY(ref_system_id));
The first CREATE statement gives the error -
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ref_system_id INT UNSIGNED NOT NULL, ref_tag ' at line 2
and the second create statement gives -
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ref_system_id INT UNSIGNED NOT NULL AUTO_INCREMENT, ' at line 1
I can't figure out what is wrong with these two CREATE statements when all of the others in the schema work fine. Can anyone see what the problem is?
It seems to be an issue with the pulled git, i have the same issue, looking at linuxquestions: https://www.linuxquestions.org/questions/slackware-14/barnyard2-setup-4175616443/
It seems to be working if you directly paste the output of the file from GIT: https://github.com/firnsy/barnyard2/blob/master/schemas/create_mysql
Tried to manually add lines 53 and 58 and that seemed to work for me.
EDIT:
after further searching it seems that the DOC we are using from SNORT is referring to a forked version of barnyard2 by binf:
https://github.com/binf/barnyard2/tree/bug-fix-release
When looking at the git from the original dev firnsy we see he added a comment to the file:
https://github.com/firnsy/barnyard2/tree/master/schemas
EDIT2:
Ahw man, now we get errors in Barnyard when trying to run it:
[SystemPullDataStore()]: Failed exeuting query [SELECT ref_system_id, ref_system_name FROM reference_system;] , will retry
When trying it Mariadb:
MariaDB [snort]> select ref_system_id, ref_system_name from reference_system;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ref_system_id, ref_system_name from reference_system' at line 1
When trying with backticks:
MariaDB [snort]> select `ref_system_id`, ref_system_name from
reference_system;
+---------------+-----------------+
| ref_system_id | ref_system_name |
+---------------+-----------------+
| 1 | Manually_Added |
+---------------+-----------------+
1 row in set (0.00 sec)
seems to be an issue with compilation:
https://github.com/firnsy/barnyard2/issues/178
I am dropping mariadb and going to use postgreSQl. hopefully with a little more luck there.

Error near LIMIT but only when I use colons

I'm trying to run this query:
SELECT wins FROM players WHERE auth = '[U:1:123456789]' LIMIT 1;
But I get the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1
However, when I remove LIMIT 1 it works.
If I change the query to look like this, it also works:
SELECT wins FROM players WHERE auth = '[123456789]' LIMIT 1;
I'm very confused, what am I doing wrong? It seems like colons just break the query.
Edit: CREATE TABLE
CREATE TABLE `players` (
`auth` varchar(32) NOT NULL,
`name` varchar(32) NOT NULL DEFAULT '< blank >',
`wins` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`auth`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Three Four options (starting with the most likely):
0) Your version of your SQL Database or your version of PHPMyAdmin is very, very out of date and should be updated ASAP.
1) You're running the SQL on a PHP PDO interface which uses Colons to mark an input variable. See here.
2) MariaDb uses colons as special characters and they need to be properly escaped.
3) Somehow you have two LIMITs (as mentoned by Tadman, that PhpMyAdmin or your own PHP interface is adding a LIMIT) when one is not needed.
It ended up being an issue with phpMyAdmin's (the client I used to run the queryh) SQL parser.
As of 4.7.1 of phpMyAdmin, it should be fixed.
https://github.com/phpmyadmin/sql-parser/commit/4f85b6d8d3a3ddcf6ff216c116cf305978e9a3d2

MySQL Can't create Table i don't know why

I always get this error when i try to create a Table:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE wolveserver.new_table (
);
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.new_table (
)' at line 1
SQL Statement:
CREATE TABLE wolveserver.new_table (
)
I gotta say i'm a total Noob, but i wanna fix this.
You will need to define your column inside the parenthesis:
CREATE TABLE wolveserver.new_table
(col1 int,
col2 varchar(50)
);
This is rather a basic question, so you should try to google this before asking a question here in SO. To learn more about CREATE TABLE syntax for MySQL, visit here.

SQL syntax error maybe because of the version I use

On phpMyAdmin, when I create this table the SQL is executed correctly but when I add all the sql code
I get an error saying
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLEbuying(CustomerIDint(10) unsigned NOT NULL,PurchaseI' at line 14`"
Why is that and how can I fix it ?
You have to use a semicolon ; after each CREATE TABLE sometable ( ) statement.
CREATE TABLE `table1` (
...
);
CREATE TABLE `table2` (
...
);
If you are running multiple SQL statements, they need to be ended with a ; (semi-colon)

Working MySQL Query fails in Perl

I have a query, which when run from within phpMyAdmin, it works, however when integrated within a website written in Perl, it fails and throws errors.
Both are run with a connection to the same database, and from coding/formatting side when integrated into the Website, all is correct and the same as other queries.
Any help with this would be much appreciated - Thanks!
MySQL Query:
CREATE TEMPORARY TABLE tmp_lecture_days (
timeslot_id int(50)
);
INSERT INTO tmp_lecture_days (timeslot_id)
SELECT DISTINCT tab_appointment.timeslot_id
FROM tab_appointment WHERE lecture_id = '1115';
SELECT COUNT(timeslot_id)
FROM tmp_lecture_days;
MySQL Query in Perl:
$query
= &statement_database(
"CREATE TEMPORARY TABLE tmp_lecture_days (
timeslot_id int(50)
);
INSERT INTO tmp_lecture_days (timeslot_id)
SELECT DISTINCT tab_appointment.timeslot_id
FROM tab_appointment WHERE lecture_id = '1115';
SELECT COUNT(timeslot_id)
FROM tmp_lecture_days;");
my ($days) = $query->fetchrow_array;
Error Log:
7.3.2011 10:14:12 error You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near ';
7.3.2011 10:14:12 error INSERT INTO tmp_lecture_days (timeslot_id)
7.3.2011 10:14:12 error SELECT DISTINCT tab_appoi' at line 3
If you are using DBI for running the query, you are not allowed to put more than one statement into it. Try putting CREATE TABLE ... into one query and INSERT ... into another.
I think the following query is equivalent to the 3 queries:
SELECT COUNT(DISTINCT timeslot_id) FROM tab_appointment
WHERE lecture_id = '1115'
For more details, check the MySQL reference manual for COUNT() here.