正如标题所示,SQLite中Python '%s %s' % (first_string, second_string)
的等效物是什么?我知道我可以像first_string || " " || second_string
那样连接,但它看起来非常难看。SQLite等价于Python的“'%s%s'%(first_string,second_string)”
回答
没有一个。
我可以理解不喜欢first_string || ' ' || second_string
,但这是等价的。标准SQL(SQLite在这方面的说法)并不是世界上最漂亮的字符串操作语言。您可以尝试将查询结果返回到其他语言(例如,您似乎喜欢的Python)并在其中进行串联;通常最好不要在数据库层中进行“演示”(当然,使用串联结果作为搜索的对象并不是一个好主意;这使得不可能使用索引进行优化!)
我并不完全确定你在找什么,但它可能是group_concat
aggregate function。
你确定你不是在寻找参数替换吗?
直接从sqlite3 module documentation:
相反,使用DB-API的参数替换。放?作为一个占位符,无论你想使用一个值,然后提供一个值的元组作为游标的execute()方法的第二个参数。 (其他数据库模块,可以使用不同的占位符,如%S或:1)
例如:
# Never do this -- insecure!
symbol = 'IBM'
c.execute("... where symbol = '%s'" % symbol)
# Do this instead
t = (symbol,)
c.execute('select * from stocks where symbol=?', t)
# Larger example
for t in [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSOFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]:
c.execute('insert into stocks values (?,?,?,?,?)', t)
不,我的意思是在常规查询的'SELECT'部分执行操作。 (例如连接两列值) – c00kiemonster 2010-10-20 12:45:41
好吧,我只是想“以防万一......”;-) – Steven 2010-10-20 14:12:33
注意,你可以创建自己的SQL级功能。例如,你可以有这样的Python功能:
def format_data(one, two):
return "%s %s" % (one, two)
使用pysqlite的create_function或APSW的createscalarfunction告诉SQLite的了。然后,你可以做查询,像这样:
SELECT format_data(col1, col2) FROM table WHERE condition;
SELECT * from TABLE where col1 = format_data('prefix', col2);
因此你可以把格式化逻辑在你舒适的可读的Python代码,同时保持SQL简单而清楚地表明意图。
- 1. 为什么不是[\\ s *]等同于\\ s *?
- 2. NSCoding V/S sqlite的
- 3. Python 3 - [s for s in subsets(S)] and yield
- 4. Java与Python的“Got value:%s”%变量的等价性?
- 5. 什么是Perl的ucfirst()或s /// e的Python等价物?
- 6. Python的 - 通过dict`s字典价值
- 7. % - #s标签等效
- 8. NRZ-S与Python
- 9. 虽然循环python,s = s + 1
- 10. Apache-Error:[file“apache2_util.c”] [line 271] [level 3] [client%s] ModSecurity:%s%s [uri“%s”]%s
- 11. 什么是Ruby的等效Python的`s =“hello,%s。%s是哪里?” %( “约翰福音”, “玛丽”)`
- 12. python string u“%(word)s”
- 13. 格式符 - %s%S
- 14. 差(/ \ S /)和$ .TYPE(/ \ s /)
- 15. 哪个更快s + ='a'或s = s +'a'python
- 16. /\A[^\\s][email protected]([^@\s]+\.++[^@\s]+\z/含义?
- 17. Python等价于ignoreboth:erasedups
- 18. 在Perl中,s/^ \ s + //和s/s + $ //有什么区别?
- 19. 的Python /熊猫/ PyMySQL - %S synthax
- 20. 在C#中使用SQLite的语用-S
- 21. “S”附近的SQLite语法不正确
- 22. 的Python - sqlite的 - OperationalError:近“S”:语法错误
- 23. Python等价于Mathematica的ArrayPlot?
- 24. PHP等价于Python的requests.get
- 25. Python等价于Mathematica的“LaguerreL”
- 26. python等价于ruby的__method__?
- 27. C#等价于python的struct.pack
- 28. Ruby等价于Python的DictWriter?
- 29. C++等价于Python的doctests?
- 30. C#等价于Python的os.path.exists()?
确实如此,但python字符串格式化比较灵活。无论如何,我会坚持更丑陋的方式。 – c00kiemonster 2010-10-20 12:46:54