sqlite用新路径替换每个指定的子路径
以下是 sqlite3 中 track_locations 表的原始输出:
表 track_locations
属性位置 '/Users/marcosvelazquez/Documents/Music/loops/Manuel Tur - 123 BPM.mp3'
属性目录 '/Users/marcosvelazquez/Documents/Music/loops'*
我想要一个更改的查询
'/Users/marcosvelazquez/Documents'
<强>路径
'/home/marc/Music'
所以我的新输出将是:
表track_locations
属性位置 '/home/marc/Music/Music/loops/Manuel Tur - 123 BPM.mp3'
属性目录 '/home/marc/Music/Music/loops'*
import sqlite3
con = sqlite3.connect('mixxxdb.sqlite')cur = con.cursor()
for row in cur.execute('''SELECT location, directory FROM track_locations'''):
rowTuple = row
newLocation = ''
newDir = ''
newTuple = ()
# Modifies the path for both location and directory
newLocation = rowTuple[0]
newLocation = newLocation.split('/Users/marcosvelazquez/Documents')
newLocation = '/home/marc/Music' + newLocation[1]
print(newLocation)
newDir = rowTuple[1]
newDir = newDir.split('/Users/marcosvelazquez/Documents')
newDir = '/home/marc/Music' + newDir[1]
print(newDir)
cur.execute('''UPDATE track_locations SET location = newLocation''')
cur.execute('''UPDATE track_locations SET directory = newDir''')
进行了几次搜索,但没有找到答案。然后我尝试使用 sqlite。这是我的查询:
UPDATE track_locationsSET location = '/home/marc/Music' | ltrim(location,'/Users/marcosvelazquez/Documents')
WHERE location IS NOT NULL;
不幸的是,这也不起作用。
帮助将不胜感激。谢谢!
Here is original output from track_locations table in sqlite3:
Table track_locations
attribute location
'/Users/marcosvelazquez/Documents/Music/loops/Manuel Tur - 123 BPM.mp3'
attribute directory
'/Users/marcosvelazquez/Documents/Music/loops'*
I want a query that changes the
'/Users/marcosvelazquez/Documents'
path to
'/home/marc/Music'
So my new output would be:
Table track_locations
attribute location
'/home/marc/Music/Music/loops/Manuel Tur - 123 BPM.mp3'
attribute directory
'/home/marc/Music/Music/loops'*
import sqlite3
con = sqlite3.connect('mixxxdb.sqlite')cur = con.cursor()
for row in cur.execute('''SELECT location, directory FROM track_locations'''):
rowTuple = row
newLocation = ''
newDir = ''
newTuple = ()
# Modifies the path for both location and directory
newLocation = rowTuple[0]
newLocation = newLocation.split('/Users/marcosvelazquez/Documents')
newLocation = '/home/marc/Music' + newLocation[1]
print(newLocation)
newDir = rowTuple[1]
newDir = newDir.split('/Users/marcosvelazquez/Documents')
newDir = '/home/marc/Music' + newDir[1]
print(newDir)
cur.execute('''UPDATE track_locations SET location = newLocation''')
cur.execute('''UPDATE track_locations SET directory = newDir''')
Did a couple searches but did not find the answer. So then I tried with sqlite. Here is the query I have:
UPDATE track_locationsSET location = '/home/marc/Music' | ltrim(location,'/Users/marcosvelazquez/Documents')
WHERE location IS NOT NULL;
This unfortunetly did not work either.
Help would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为sqlite函数 replace()是您正在寻找的东西:
I think the SQLite function replace() is what you are looking for :