sql-parametrized-query

Is there any safe way to parameterize database names in MySQL queries?

孤者浪人 提交于 2019-12-01 11:18:10
I'm writing a little python script to help me automate the creation of mysql databases and associated accounts for my personal projects. Part of this script is a function that takes the database name as a string, then goes to create the database. def createDB(dbConn, dbName): import MySQLdb c = dbConn.cursor() query = """CREATE DATABASE %s;"""; c.execute(query, (dbName,)) This doesn't work because MySQL's CREATE DATABASE asks for the unquoted name of the database, as in CREATE DATAbASE test_db but my code that attempts to safely insert the user provided db name into the query creates: CREATE

Python MYSQL update statement

99封情书 提交于 2019-11-26 21:56:28
I'm trying to get this Python MYSQL update statement correct(With Variables): cursor.execute ("UPDATE tblTableName SET Year=%s" % Year ", Month=%s" % Month ", Day=%s" % Day ", Hour=%s" % Hour ", Minute=%s" Minute "WHERE Server=%s " % ServerID) Any ideas where I'm going wrong? It should be : cursor.execute (""" UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=%s """, (Year, Month, Day, Hour, Minute, ServerID)) You can also do it with basic string manipulation, cursor.execute ("UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server='

In PHP with PDO, how to check the final SQL parametrized query? [duplicate]

三世轮回 提交于 2019-11-26 16:06:30
This question already has an answer here: Getting raw SQL query string from PDO prepared statements 16 answers In PHP, when accessing MySQL database with PDO with parametrized query, how can you check the final query (after having replaced all tokens)? Is there a way to check what gets really executed by the database? So I think I'll finally answer my own question in order to have a full solution for the record. But have to thank Ben James and Kailash Badu which provided the clues for this. Short Answer As mentioned by Ben James: NO . The full SQL query does not exist on the PHP side, because

Parametrized PDO query and `LIMIT` clause - not working [duplicate]

半世苍凉 提交于 2019-11-26 09:58:00
问题 This question already has answers here : How to apply bindValue method in LIMIT clause? (9 answers) Closed 4 years ago . I have query like this: SELECT imageurl FROM entries WHERE thumbdl IS NULL LIMIT 10; It works perfectly with PDO and MySQL Workbench (it returns 10 urls as I want). However I tried to parametrize LIMIT with PDO: $cnt = 10; $query = $this->link->prepare(\" SELECT imageurl FROM entries WHERE imgdl is null LIMIT ? \"); $query->bindValue(1, $cnt); $query->execute(); $result =

Python MYSQL update statement

徘徊边缘 提交于 2019-11-26 08:04:41
问题 I\'m trying to get this Python MYSQL update statement correct(With Variables): cursor.execute (\"UPDATE tblTableName SET Year=%s\" % Year \", Month=%s\" % Month \", Day=%s\" % Day \", Hour=%s\" % Hour \", Minute=%s\" Minute \"WHERE Server=%s \" % ServerID) Any ideas where I\'m going wrong? 回答1: It should be: cursor.execute (""" UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=%s """, (Year, Month, Day, Hour, Minute, ServerID)) You can also do it with basic

In PHP with PDO, how to check the final SQL parametrized query? [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-26 04:42:09
问题 This question already has answers here : Getting raw SQL query string from PDO prepared statements (16 answers) Closed 3 years ago . In PHP, when accessing MySQL database with PDO with parametrized query, how can you check the final query (after having replaced all tokens)? Is there a way to check what gets really executed by the database? 回答1: So I think I'll finally answer my own question in order to have a full solution for the record. But have to thank Ben James and Kailash Badu which

How do I create a parameterized SQL query? Why Should I?

瘦欲@ 提交于 2019-11-26 01:18:06
问题 I\'ve heard that \"everyone\" is using parameterized SQL queries to protect against SQL injection attacks without having to vailidate every piece of user input. How do you do this? Do you get this automatically when using stored procedures? So my understanding this is non-parameterized: cmdText = String.Format(\"SELECT foo FROM bar WHERE baz = \'{0}\'\", fuz) Would this be parameterized? cmdText = String.Format(\"EXEC foo_from_baz \'{0}\'\", fuz) Or do I need to do somethng more extensive