我很难找到如何在LIKE%var%startment中执行Python变量:%s或({url})的答案。
百分号会导致我出现问题...语法错误
以下是我正在尝试查询的SQLite数据库的示例转储:
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "user_cats" (
`category` TEXT,
`uploader` TEXT,
`folder` TEXT,
`playlists` TEXT,
`playlist_image` TEXT,
`save_directory` TEXT
);
INSERT INTO user_cats VALUES('Comics','ComicsExplained','DC Rebirth','[''New to DC Comics? Start here!'']',NULL,'%USERPROFILE%\Videos\Online Videos\Comics\ComicsExplained\');
INSERT INTO user_cats VALUES('Comics','Comicstorian',NULL,NULL,NULL,NULL);
INSERT INTO user_cats VALUES('Video Games','IGN','Daily Fix','[''Daily Fix'']',NULL,'%USERPROFILE%\Videos\Online Videos\Video Games\IGN\Daily Fix\');
INSERT INTO user_cats VALUES('Comics','Marvel Entertainment','Marvel Top 10','[''Marvel Top 10'']',NULL,'%USERPROFILE%\Videos\Online Videos\Comics\Marvel Entertainment\');
INSERT INTO user_cats VALUES('','ScrewAttack!',NULL,NULL,NULL,NULL);
COMMIT;
我想做的是使用youtube-dl检索上传者和播放列表标题的公式(我可能会变成一个对象方法)。
然后使用数据库将文件路由到不同的保存位置,并且 - 可能保存其他信息。 但是,我遇到SELECT语句和百分号的问题。错误取决于我如何编辑语句 - 各种语法或意外的字符等。(它可能没有帮助键入我得到的每一个错误 - 这是变量赋值的问题 - 而不是语句。
如果我使用简单的查询SQLite(在Python之外)...
SELECT save_directory FROM user_cats WHERE uploader='IGN' AND playlists LIKE '%ail%';
宾果。的工作原理。
def save_dir (uploader, playlist):
query = "SELECT save_directory FROM user_cats WHERE uploader=({ul}) AND playlists LIKE '%%({pl})%%'.format(ul = uploader, pl = playlist)".format(ul=uploader, pl=playlist))
c.execute(query)
all_rows = c.fetchall()
print('1):', all_rows)
答案 0 :(得分:-1)
你可以这样格式化:
>>> uploader, playlist = 'someUploader', 'somePlaylist'
>>> query ='SELECT save_directory FROM user_cats WHERE uploader={0:s} AND playlists LIKE %{1:s}'
>>> query.format(uploader, playlist)
'SELECT save_directory FROM user_cats WHERE uploader=someUploader AND playlists LIKE %somePlaylist'
甚至这样:
>>> query ='SELECT save_directory FROM user_cats WHERE uploader=%(ul)s AND playlists LIKE %(pl)s'
>>> query % {'ul': 'someUploader', 'pl': '%somePlaylist%'}
'SELECT save_directory FROM user_cats WHERE uploader=someUploader AND playlists LIKE %somePlaylist%'
答案 1 :(得分:-1)
它并不漂亮,但我最终通过将字符串添加到一起来查询工作,而不是尝试替换值。
def save_dir(上传者,播放列表):
save_dir('IGN','Daily Fix')
1): [('%USERPROFILE%\\Videos\\Online Videos\\Video Games\\IGN\\Daily Fix',)]
[Finished in 0.116s]
输出......
{{1}}
最后-___-