好的,所以我不是那种经验丰富的Python.
我有以下Python代码:
cursor.execute("INSERT INTO table VALUES var1, var2, var3,")
Run Code Online (Sandbox Code Playgroud)
where var1是整数,var2&var3是字符串.
如何在没有python的情况下编写变量名,包括它们作为查询文本的一部分?
什么是在mysql上运行查询的最安全的方法,我知道MySQL和SQL注入涉及的危险.
但是我不知道如何运行查询以防止注入其他用户(webclients)可以操作的变量.我曾经写过自己的转义函数,但显然这是"未完成".
我应该使用什么以及我应该如何使用它来查询并通过python安全地插入MySQL数据库而不会冒险注入mysql?
我有以下代码,使用pscyopg2:
sql = 'select %s from %s where utctime > %s and utctime < %s order by utctime asc;'
data = (dataItems, voyage, dateRangeLower, dateRangeUpper)
rows = cur.mogrify(sql, data)
Run Code Online (Sandbox Code Playgroud)
这输出:
select 'waterTemp, airTemp, utctime' from 'ss2012_t02' where utctime > '2012-05-03T17:01:35+00:00'::timestamptz and utctime < '2012-05-01T17:01:35+00:00'::timestamptz order by utctime asc;
Run Code Online (Sandbox Code Playgroud)
当我执行它时,它会失败 - 这是可以理解的,因为表名周围的引号是非法的.
有没有办法合法地将表名作为参数传递,或者我是否需要执行(显式警告)字符串连接,即:
voyage = 'ss2012_t02'
sql = 'select %s from ' + voyage + ' where utctime > %s and utctime < %s order by utctime asc;'
Run Code Online (Sandbox Code Playgroud)
为任何见解干杯.
运行此脚本时:
#! /usr/bin/env python
import MySQLdb as mdb
import sys
class Test:
def check(self, search):
try:
con = mdb.connect('localhost', 'root', 'password', 'recordsdb');
cur = con.cursor()
cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search )
ver = cur.fetchone()
print "Output : %s " % ver
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
finally:
if con:
con.close()
test = Test()
test.check("test")
Run Code Online (Sandbox Code Playgroud)
我得到一个错误:
./lookup
Traceback (most recent call last):
File "./lookup", line 27, in <module>
test.check("test")
File "./lookup", …Run Code Online (Sandbox Code Playgroud) 在Python中使用psycopg2中的execute()指定参数时,如下所示:
cursor.execute('SELECT * FROM %s', ("my_table", ))
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
psycopg2.ProgrammingError: syntax error at or near "'my_table'"
LINE 1: SELECT * FROM 'my_table'
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?看起来psycopg2正在向查询添加单引号,这些单引号导致语法错误.
如果我不使用参数,它可以正常工作:
cursor.execute('SELECT * FROM my_table')
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 psycopg2 批量插入 postgres 数据库。我正在使用 %s 和一个元组列表,但它失败并出现以下错误:
File ".../python3.6/site-packages/psycopg2/extras.py", line 1183, in execute_batch
sqls = [cur.mogrify(sql, args) for args in page]
File ".../python3.6/site-packages/psycopg2/extras.py", line 1183, in <listcomp>
sqls = [cur.mogrify(sql, args) for args in page]
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import psycopg2
import psycopg2.extras
conn = psycopg2.connect(
database='mydb',
user='name',
password='pass')
cur = conn.cursor()
query = "INSERT INTO my_table (tweet_id, user_id, time, text,
reply_to_user_id, reply_to_tweet_id, reply_to_handle, is_retweet,
is_quote, quote_usr_id, quote_usr_handle, quote_id, quote_text,
retweet_usr_id, retweet_usr_handle, retweet_id, longitude, latitude, …Run Code Online (Sandbox Code Playgroud)