在 raw_input 给出的字符串周围添加单引号
Adding single quotes around a string given from raw_input
我正在根据给定输入创建 SQL 命令:
def do_add(self, table, full_sql_command="INSERT INTO {} VALUES ({})"):
""" Add a column to a specified table name """
add_command = raw_input("INSERT INTO {} VALUES ".format(table))
self.__create_sql_command(full_sql_command, table, add_command.split(" "))
def __create_sql_command(self, full, tablename, addable):
print full.format(tablename, ', '.join(addable))
我需要这个输出的是INSERT INTO <table-name> VALUES ('<val-1>', '<val-2>', '<etc..>')
截至目前,我得到以下输出:
INSERT INTO inventory VALUES test test test
# <= INSERT INTO inventory VALUES (test, test, test)
如何获取要添加的值的引号?
如果有人好奇,这就是我最后做的:
永远不要在生产中这样做它极易受到 SQL 注入
def __create_sql_command(self, full, tablename, addable):
return full.format(tablename, ', '.join(map(lambda x: "'" + x + "'", addable)))
#<= CREATE TABLE test ('test1', 'test2', 'test3')
我不是 SQL 人,但如果您输入 table 数据并用 space 分隔,那么一点 for 循环就可以完成您的工作,
说:
l=raw_input('Enter Table contents separated by space.').split(' ')
l1='('
for i in l:l1=l1+i+', '
l1=l1[:len(l1-3)] #for removing extra comma and space left after loop
l1=l1+')'
然后随心所欲地使用 l1。您可以使用私有属性或其他东西,以获得更多保护!
不要将参数用引号括起来,而是使用参数化查询。它实际上会根据需要透明地添加引号,并且不会因为例如问题而出现问题。输入本身的引号,无论是有意的(SQL 注入攻击)还是无意的。
这是一个参数化查询的例子。
cursor.execute("INSERT INTO table VALUES (?, ?, ?)", (var1, var2, var3))
如您所见,您需要将两个单独的参数传递给 execute()
、查询模板和值,因此需要对代码进行一些调整。
注意:不同的数据库模块接受different placeholders for the parametrized query. sqlite3
,例如,接受?
如上,但也允许命名参数(传入字典代替一个元组):
cursor.execute("INSERT INTO table VALUES (:who, :age)", {"who":"Bill", "age": 33})
我正在根据给定输入创建 SQL 命令:
def do_add(self, table, full_sql_command="INSERT INTO {} VALUES ({})"):
""" Add a column to a specified table name """
add_command = raw_input("INSERT INTO {} VALUES ".format(table))
self.__create_sql_command(full_sql_command, table, add_command.split(" "))
def __create_sql_command(self, full, tablename, addable):
print full.format(tablename, ', '.join(addable))
我需要这个输出的是INSERT INTO <table-name> VALUES ('<val-1>', '<val-2>', '<etc..>')
截至目前,我得到以下输出:
INSERT INTO inventory VALUES test test test
# <= INSERT INTO inventory VALUES (test, test, test)
如何获取要添加的值的引号?
如果有人好奇,这就是我最后做的:
永远不要在生产中这样做它极易受到 SQL 注入
def __create_sql_command(self, full, tablename, addable):
return full.format(tablename, ', '.join(map(lambda x: "'" + x + "'", addable)))
#<= CREATE TABLE test ('test1', 'test2', 'test3')
我不是 SQL 人,但如果您输入 table 数据并用 space 分隔,那么一点 for 循环就可以完成您的工作, 说:
l=raw_input('Enter Table contents separated by space.').split(' ')
l1='('
for i in l:l1=l1+i+', '
l1=l1[:len(l1-3)] #for removing extra comma and space left after loop
l1=l1+')'
然后随心所欲地使用 l1。您可以使用私有属性或其他东西,以获得更多保护!
不要将参数用引号括起来,而是使用参数化查询。它实际上会根据需要透明地添加引号,并且不会因为例如问题而出现问题。输入本身的引号,无论是有意的(SQL 注入攻击)还是无意的。
这是一个参数化查询的例子。
cursor.execute("INSERT INTO table VALUES (?, ?, ?)", (var1, var2, var3))
如您所见,您需要将两个单独的参数传递给 execute()
、查询模板和值,因此需要对代码进行一些调整。
注意:不同的数据库模块接受different placeholders for the parametrized query. sqlite3
,例如,接受?
如上,但也允许命名参数(传入字典代替一个元组):
cursor.execute("INSERT INTO table VALUES (:who, :age)", {"who":"Bill", "age": 33})