This question already has an answer here:
How to get Description of MySQL Table in GoLang
(1 answer)
Closed 3 years ago.
How do I retrieve data sets from non-standard MySQL "show" statement using golang? For example, "show tables", "show variables", "show engine innodb status". etc.
I cannot find any information to retrieve the result set from mysql "show" statement in Golang. Using either database/sql package or sqlx package is fine.
Got it! Here is the code. It worked!
var r1, r2, r3 string
row := db.QueryRow("show engine innodb status")
err = row.Scan(&r1, &r2, &r3)
Related
This question already has answers here:
MySQL You are using safe update mode and you tried to update a table without a WHERE
(2 answers)
Closed 2 years ago.
I'm trying to add a numeric value to a column whose "AuthDate" is "06/05/20" and "Time" is "1P -2P".
I ran the following query:
update main
set calls = 2072
where authdate = '06/05/20'
and time = '1P - 2P';
*main = table name
calls = column name
but I get error code 1175 You are using safe update mode
Does anyone know why I get the error?
Your database or session has option sql_safe_updates enabled. Amongst other things, this forbids update that do not have a where clause, or that have a where clause whose columns are not keys (are not indexed). Even if there is an index, but the optimize chooses not to use it, the query is also aborted.
This is more of an option that prevents mistakes from beginners. If you really know what you are doing, you can disable the option at session level:
set sql_safe_updates = 0;
Side note: I am suspicious about condition authdate = '06/05/20':
if autdate is of a date-like datatype, then you should give it a legitimate date string, like: authdate = '2020-06-05' (or '2020-05-06'?)
if it's a string then... consider storing it as a date; storing dates as string is bad practice for many reasons, and should be avoided
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
My web host has upgraded its servers. The newer 5.7.27 version of MySQL that they installed has LOAD DATA LOCAL INFILE disabled by default, resulting in Error 1148 when I try to execute the command. Unfortunately I can't start or stop the MySQL instance as that is under the control of the web host. What are some workarounds or alternate methods that will allow me to import data with the least effort? All the data I want to import are currently in TSV (tab separated value) format, but I could switch to CSV or something else if required. I have Workbench installed as well if it helps.
The problem is basically the same as this one, except I cannot access and reconfigure the server (the selected answer to that question).
I'd write a Python script to take your TSV input, and use it to generate INSERT statements in a loop. Each statement would handle perhaps 100-200* new rows. Then it would execute those statements.
Run it on the same server. Do it in a transaction so you don't make a mess on your first few tries if there are errors.
There you have it: TSV import.
* Or, well, whatever you want. Doing them one at a time will be slow (because there is a small overhead associated with the execution of each SQL statement), but you probably can't just dump them all into a single INSERT unless the amount of information is small. Check your server settings/limits, and come up with a reasonable batch size for your use case. For <2000 rows, and reasonably "short" row data, 100-200 rows per statement would usually be appropriate.
Pseudo-code:
batchSize = 100
buffer = []
handleInput():
for each line in tsvFile:
data = parse(line)
add data to buffer
if size(buffer) > batchSize:
flushBuffer
if size(buffer) > batchSize:
flushBuffer
flushBuffer:
str = "INSERT INTO tbl (col1, col2, col3) VALUES"
for each row in buffer:
if !str.empty():
str += ","
str += "(" + row[col1] + ", " + row[col2] + ", " + row[col3];
executeSqlStatement(str)
buffer = []
This question already has answers here:
MySQL wait_timeout Variable - GLOBAL vs SESSION
(3 answers)
Differences between SET var = 'abc' and SET GLOBAL var = 'abc'?
(1 answer)
Closed 3 years ago.
I have two questions about MySQL usage:
I installed mysql5.7 by default options.
then I connect to MySQL by MySQL query browser:
1) if I run "SET autocommit='OFF'", then I use "show variables like 'autocommit';" to check the value, but I still get the result:
autocommit='ON'.
Only if I run "SET global autocommit='OFF';" then I can see the value has been changed, why?
2) with autocommit='ON' , I want to lock a table by:
lock tables xxx write
but I can still query this table from another session.
why?
This question already has answers here:
Perl DBI - run SQL Script with multiple statements
(4 answers)
Closed 8 years ago.
At the moment, I am running multiple statements on MYSQL as below;
my $sth1 = $dbh->prepare("ALTER TABLE whatever....");
my $sth2 = $dbh->prepare("UPDATE whatever....");
my $sth3 = $dbh->prepare("ALTER TABLE whatever....");
my $sth4 = $dbh->prepare("DROP TABLE whatever....");
my $sth5 = $dbh->prepare("DROP TABLE whatever....");
$sth1->execute;
$sth1->finish;
$sth2->execute;
$sth2->finish;
$sth3->execute;
$sth3->finish;
$sth4->execute;
$sth4->finish;
$sth5->execute;
$sth5->finish;
This code works fine.
However, I have about more than 50 such queries. So you can imagine the magnitude of above lines. What I pasted above is just 5 queries.
Question:
Is there a better elegant way of running multiple MySQL queries/Statements using Perl DBI ?
At the very least, you should just iterate of your sql strings. Also would be a good idea to add or die to your execute methods:
my #sql = (
q{ALTER TABLE whatever....},
q{UPDATE whatever....},
q{ALTER TABLE whatever....},
q{DROP TABLE whatever....},
q{DROP TABLE whatever....},
);
for (#sql) {
my $sth = $dbh->prepare($_);
$sth->execute or die $dbh->errstr;
}
DBD::mysql has a parameter mysql_multi_statements:
As of MySQL 4.1, support for multiple statements separated by a semicolon (;) may be enabled by using this option. Enabling this option may cause problems if server-side prepared statements are also enabled.
This question already has answers here:
MySQL find_in_set with multiple search string
(7 answers)
Closed 9 years ago.
The following query works good.
SELECT FIND_IN_SET('b','a,b,c,d');
// output -> 2
I need to fetch the record having multiple options. Just for an example,
SELECT FIND_IN_SET('b,c','a,b,c,d');
// output -> ??????????
Please how do i get the record with multiple selection option "b,c" in "a,b,c,d".
mysql function find_in_set can search only for one string in a set of strings.
the second its not a string in set of strings
take a look here
edit:
to change the mode
This can be done in two ways...
1- Open your "my.ini" file within the MySQL installation directory, and look for the text "sql-mode".
Find:
Code:
Set the SQL mode to strict sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
Code:
Set the SQL mode to strict sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Or
2- You can run an SQL query within your database management tool, such as phpMyAdmin:
Code:
SET ##global.sql_mode= '';