我試圖寫一個記錄到這裏,我定義的表jobs
爲MySQL數據庫:插入到MySQL從Python中 - 錯誤
CREATE TABLE jobs(
job_id INT NOT NULL AUTO_INCREMENT,
job_title VARCHAR(300) NOT NULL,
job_url VARCHAR(400) NOT NULL,
job_location VARCHAR(150),
job_salary_low DECIMAL(25) DEFAULT(0),
job_salary_high DECIMAL(25), DEFAULT(0),
company VARCHAR(150),
job_posted DATE,
PRIMARY KEY (job_id)
);
我用來測試的代碼是:
cur.execute("INSERT INTO jobs VALUES(DEFAULT, '"+jobTitle+"','"
+jobHref+"',"+salaryLow+","+salaryHigh+",'"+company+"',"
+jobPostedAdjusted+"','"+salaryCurrency+"';")
print(cur.fetchall())
我得到的錯誤是:
pydev debugger: starting
Traceback (most recent call last):
File "C:\Users\Me\AppData\Local\Aptana Studio 3\plugins\org.python.pydev_2.7.0.2013032300\pysrc\pydevd.py", line 1397, in <module>
debugger.run(setup['file'], None, None)
File "C:\Users\Me\AppData\Local\Aptana Studio 3\plugins\org.python.pydev_2.7.0.2013032300\pysrc\pydevd.py", line 1090, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\Me\Documents\Aptana Studio 3 Workspace\PythonScripts\PythonScripts\testFind.py", line 25, in <module>
+jobPostedAdjusted+"','"+salaryCurrency+"';")
TypeError: cannot concatenate 'str' and 'float' objects
插入此記錄的最佳方式是什麼?謝謝。
請不要將查詢建立爲字符串,請使用參數化查詢,這樣您就不會將自己打開到sql注入 – dm03514
@ dm03514:感謝您指出了這一點。 –