“u”在输出开始处(Sqlite、Python)

2024-09-30 18:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我真的很困惑sqlite部分,我打印出我的数据库,因为我一直在每一行的开头都有一个“u”,但是它周围没有“”,我假设它不被接受为字符串?这只发生在这一个程序上(我也在另一个程序上测试过)。在

我检查了很多次代码(当我用“SQLite数据库浏览器”打开它时,它不会出现在数据库中)

代码如下:

#!/usr/bin/python

import sqlite3
import os

def list_n_convert(way):
    if way == 'mov':#movie search
        output = os.popen("find '/home/fugo/' -name '*.mp4' -printf '%f\n'").read()
    word = ''
    lyst = []
    for letter in output:
        if letter != '\n':
            word += str(letter)
        else:
            lyst.append(word)
            word = ''
    return lyst

#Loop to create entries
def entry_maker(lyst, column):
    for count in range(len(lyst)):
        cur.execute("INSERT OR IGNORE INTO myliltable ({}) VALUES (?)".format(column), (lyst[count],))
        con.commit()

if __name__ == '__main__':

    #necessities for start-up of sql-lite
    con = sqlite3.connect('movie.db')
    cur = con.cursor()

    #Create a table, if it's non-existent
    cur.execute('CREATE TABLE IF NOT EXISTS myliltable (name TEXT PRIMARY KEY, cover TEXT, genre TEXT)')

    entry_maker(list_n_convert('mov'), 'name')

    cur.execute('SELECT * FROM myliltable')
    for row in cur.fetchall():
        print(row) #print each row in a line <--- 'ERROR' happens here

    cur.close()
    con.close()

输出如下:

^{pr2}$

我将解释程序的作用,也许它有助于找出错误:

第一个函数“list”在“my home”文件夹中搜索以“.mp4”结尾的任何文件,然后将搜索结果转换为字符串并将其添加到列表中。在

第二个方法为列表中的每个元素(每个mp4文件)向数据库中输入条目。最后,我调用函数并让每一行打印出来;这就是发生错误的地方。在


Tags: textnamein数据库forexecuteifcon