在MySQL查询中插入变量

时间:2017-02-07 16:26:13

标签: python mysql mysql-python

我是一个使用MySQL的新手,而且我遇到了一个非常复杂的(对我来说)查询......

就是这个:

numbers = [1,2,3]
for number in numbers:
            self.db.cursor().execute("""(SELECT 'bla', 'bla', 'bla', 'bla', 'bla') 
                                         UNION 
                                         (SELECT bla, bla, bla, bla, bla 
                                         FROM table 
                                         WHERE num = %s AND date = 'YY-MM-DD'
                                         INTO OUTFILE CONCAT('/var/lib/mysql-files/file_', %s, '.csv') 
                                         FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' 
                                         LINES TERMINATED BY '\r\n')""", (number, number))

当我运行查询时,我得到了这个:

ProgrammingError: (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 \'CONCAT(\'/var/lib/mysql-files/file_\', 1, \'h.csv\') FIELDS ENCLOSED BY \'"\' TERMINAT\' at line 6')

我真的认为这是因为我试图将变量插入到查询中,因为我用静态数字替换' %s ' ,它完美无缺! (WHERE num = 1/var/lib/mysql-files/file_1.csv') 我尝试过很多东西,但我什么都没有......只是为了让自己变得一团糟:(

那么,我怎样才能在这些地方正确插入变量'数字'?

这些是我访问过并试过的链接:

Using a Python variable in MySQL query

Python MySQL executemany in WHERE clause

0 个答案:

没有答案