Issue with pulling information from mysql in flask - mysql

I am working on a simple program and attempting to pull some information out of a database. The table within the database that I am using is called names. If I do something like this:
self.cursor.execute("""select * from names WHERE first_name = 'Sarah' """)
for row in self.cursor.fetchall():
print(row[0])
I get exactly what I want. But if I change that name to a variable like and do something like:
name = input('Enter First name: ')
self.cursor.execute("""select * from names WHERE first_name=%s""", (name))
I get the following error: mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s' at line 1
I know that there are other posts on this site close to my question and what I have tried from them does not seem to work. I have also tried following some tutorials and still get this error. So any help would be appreciated! Thank you! I am using mysql.connector, Python 3.4 and Flask.

Okay I found the answer myself. I had to do this: self.cursor.execute(query, (name,)) and the answer was found digging deeper and I found this site: Python MySQL Connector database query with %s fails

Related

'Cant get attributes from Select' while setting up Query for export data task in DBeaver

Hey so I'm trying to set up an export data task in DBeaver, because I want to export some data to another database and I want to query the date in beforehand. So I'm trying to use this SELECT statement
SELECT *
FROM resource
WHERE assignedteamid = 2 AND active;
which works fine on its own but when I'm trying to add this query in the Task setup screen and click on continue I get this Error
Can't get attributes from `SELECT * FROM resource WHERE assignedteamid = 2 AND active;`
Reason: SQL Error [1064] [42000]: 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 'LIMIT 0, 1' at line 4
I already tried to change up the query but I have no clue what I need to do to fix this problem. I appreciate any help.
Thank you!

Getting a formatting error on the output of any MySQL stored procedure I try

I am sure that I am missing some small but important detail, but need to help to see why I am consistantly getting an error when I add in the SELECT #output in my input like this. I have looked at many aritcles and answers but none of them are quite what I am looking at:
let connection = mysql.createConnection(config,{CLIENT_MULTI_RESULTS: true});
(this line is the issue)
**let sql = 'CALL sp_whatever(?,#usernameOut);select #usernameOut;'**
await connection.query(sql, [param1],
function(err,rows){
console.log("INSIDE MySQL1");
I am doing this in Node JS and most examples are acutally in PHP. I have not found anything that is exactly what I am looking for (why am I getting a formatting error when I set it up like other examples or tutorials?)
I am using MySQL 5.7 on my Azure LInux server and the MySQL stored procedure looks like this: (in case the issue is inside the Stored Procedure itself)
CREATE DEFINER=`someDB`#`%` PROCEDURE `GetUsername`(
IN userIdVal INT,
OUT usernameOut NVARCHAR(45)
)
BEGIN
SELECT username INTO usernameOut
FROM players
WHERE userId = userIdVal AND avatarId = 0 AND Gender IS NULL AND active = 1 ;
END
This is the error I am getting:
err.message: ER_PARSE_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 'select #username' at line 1
It turns out that my real problem was a Node JS one. I was unable to get the output because it was a second command. I had read that I needed to add in multipleStatements: true if I wanted/needed to process multiple commands. What I didn't figure out until tonigt was that it had to be added to the config file to work correctly. Works great now!

MySQL find and replace not working as expected (serialized data)

Im trying to create a MySQL search and replace script for my wp_postmeta db table.
I need to search this fields that are similar to this.
a:2:{i:0;a:3:{s:4:"name";s:15:"Personalisation";s:4:"type";s:7:"heading";s:11:"description";s:0:"";}i:1;a:13:{s:4:"name";s:8:"Initials";s:12:"title_format";s:5:"label";s:4:"type";s:11:"custom_text";s:17:"restrictions_type";s:8:"any_text";s:11:"description";s:0:"";s:18:"description_enable";i:0;s:8:"required";i:1;s:3:"min";s:1:"1";s:3:"max";s:1:"4";s:12:"restrictions";i:1;s:5:"price";s:1:"3";s:10:"price_type";s:14:"quantity_based";s:12:"adjust_price";i:1;}}
and replace the "required";i:1; with "required";i:0;
Im using phpmyadmin to create and run the script but so far I cannot get what I have written to run with the semicolons in there.
This is what ive come up with so far;
UPDATE wp_postmeta
SET meta_value = replace(meta_value, '"required";i:1;', '"required";i:0;')
WHERE meta_key = '_product_addons';
and get the error (removing the semicolons fixes this but then doesn't match anything):
#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 ''"required")' at line 1
I've tried using \ to escape them and this doesn't work, this is about as far as my knowledge goes with MySQL so hoping someone can help?
Thanks
OK so I've found out that this actually works fine but won't work when using the Simulate option, can anyone explain why this is?

Mysql function return system result

In mysql, I'm trying to create a function that will return the result of a bash command. I've created a simplified version of what I'm trying to do to make it easier to understand.
I have a file called mycmd.sh, and inside it, I have this line:
echo $1 'test'
When in mysql, I execute the sh file it is working fine
system ~/mycmd.sh 'test'
But I would like to be able to call it while doing a select in mysql, something like this
select myFunc(username) from user;
The result will be that, every username will have a "test" after it.
The problem is when I try to create the mysql function
CREATE FUNCTION myFunc(s VARCHAR(500)) RETURNS VARCHAR(505)
RETURN SYSTEM ~/mycmd.sh s;
I always get the error: ERROR 1064 (42000): 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 '~/mycmd.sh s' at line 2
I tried putting it in quote, or in a variable, or in a prepared statement, but nothing seem to work...
Anyone have an idea how to do so?
Thank you
Unfortunately what you are trying to do isn't possible without using a UDF (User Defined Plugin). The system command you're using is part of the MySQL shell, not an actual MySQL function, which is why it doesn't work in the function you're trying to define.
The only other option I can think of is to write a procedure that imitated whatever your external command was going to do.

Mysql syntax error in scrapy

Hi i am working on scrapy and writing a pipeline and in that i had a query which should write the data in to mysql database
tx.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",
(item['book_name'],
item['price'],)
)
I am getting the following errors two errors below
(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 '))' at line 2")
(1241, 'Operand should contain 1 column(s)')
I dont know whats wrong in this query but i am unable to save the data in to database.
Can any one have a idea of this.
You forgot to add % while executing
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",%
(item['book_name'],
item['price'])
)
You have added an additional comma at last remove it. following is the correct statement. Kindly try.
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s,%s,%s,%s,%s)""",
(item['book_name'],
item['price'])
)