sqlite用新路径替换每个指定的子路径

发布于 2025-01-19 06:22:13 字数 1639 浏览 0 评论 0原文

以下是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几味少女 2025-01-26 06:22:13

我认为sqlite函数 replace()是您正在寻找的东西:

UPDATE track_locations
SET location = replace(location, '/Users/marcosvelazquez/Documents', '/home/marc/Music'),
directory = replace(directory, '/Users/marcosvelazquez/Documents', '/home/marc/Music')

I think the SQLite function replace() is what you are looking for :

UPDATE track_locations
SET location = replace(location, '/Users/marcosvelazquez/Documents', '/home/marc/Music'),
directory = replace(directory, '/Users/marcosvelazquez/Documents', '/home/marc/Music')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文