Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tty Python Sqlite应用程序:Noob name错误、意外的EOF和Sqlite错误_Python_Sqlite - Fatal编程技术网

tty Python Sqlite应用程序:Noob name错误、意外的EOF和Sqlite错误

tty Python Sqlite应用程序:Noob name错误、意外的EOF和Sqlite错误,python,sqlite,Python,Sqlite,我正在将一个tcl脚本移植到python,因为我似乎无法在我的诺基亚N810上运行tclSqlite。该脚本提示输入,将它们传递给一个sqlite db,其中包含3个表:Notes、Tags和NotesXTags多对多tbl。有一些触发器阻止我多次存储任何标记。作为一名noob/爱好者,我逐行浏览了tcl脚本,并用Python行替换了每一行。不是很像python,但我是一个爱好者,在我将这一个脚本用于N810之后,我无意使用这种语言。我确实看了每个问答S.O.建议,我已经为此工作了几个小时。我至

我正在将一个tcl脚本移植到python,因为我似乎无法在我的诺基亚N810上运行tclSqlite。该脚本提示输入,将它们传递给一个sqlite db,其中包含3个表:Notes、Tags和NotesXTags多对多tbl。有一些触发器阻止我多次存储任何标记。作为一名noob/爱好者,我逐行浏览了tcl脚本,并用Python行替换了每一行。不是很像python,但我是一个爱好者,在我将这一个脚本用于N810之后,我无意使用这种语言。我确实看了每个问答S.O.建议,我已经为此工作了几个小时。我至少有三个无知的错误。模块“pythonmakenote.py”中的脚本块:

嘎吱嘎吱的一声。。。还有一些评论

import sys, tty
import sqlite3

def mn():
    conn = sqlite3.connect('/home/j...notes.sqlite')
    db = conn.cursor()
tagsofar =db.execute('select tag_text from tag')
print tagsofar

print "Enter note text, remember to let console wrap long lines"
notetxt = input("note: ") 

print "Enter 1 or more tags separated by spaces"
taglist = input("tags: ")
taglist = taglist.split(" ")

db.execute('INSERT INTO note (note_txt) VALUES (?)', notetxt)
db.commit
fknote = db.execute('select last_insert_rowid()')

#records new tags since db trigger stops dups, updates many-many tbl

for tagtxt in taglist: 
    db.execute('INSERT INTO tag VALUES (?)',tagtxt)
    db.commit
    fktag = db.execute('select rowid from tag where tag_text = (?)',tagtxt)
    db.execute('INSERT INTO fkeys VALUES (?,?)',fknote,fktag)
    db.commit
所以我做“进口蟒蛇毒药”。到现在为止,一直都还不错。我键入“mn”并得到一个错误:

>>> mn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'mn' is not defined
然后我试试这个:

>>> from pythonmakenote import mn
>>> mn
<function mn at 0xb76b2a74>
但是“mn”仍然不起作用。所以我删除了Def,复制了文件,并将其命名为“mn.py”,它可以正常工作

>>> import mn
<sqlite3.Cursor object at 0xb75fb740>
Enter note text, remember to let console wrap long lines
note: 'this is a note'<--------------------------Quotes are a MUST (but not in tcl version)
Enter 1 or more tags separated by spaces
tags: 'dev'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "mn.py", line 19, in <module>
    db.execute('INSERT INTO note (note_txt) VALUES (?)', notetxt)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 14 supplied.<-----------------------------Huh?
关于代码块标记和其他降价的S.O.指令究竟在哪里

为什么我不能在模块中定义mn并使用它?是否相关?解决我的问题

我还有其他几个def要做,比如按标签获取笔记,获取标签列表等等。。我想它们都可以放在一个模块里

我不想在注释或空格分隔的标记列表中的输入周围加引号。这可行吗?我有进口tty的东西在那里,但我没有使用它不知道如何,但我开始怀疑我将不得不学习

如果我在提示时放入3个标记(不带引号),则会出现“意外EOF”错误。为什么?

我看到字符串是不可变的,所以将列表/拆分分配给以前是字符串的变量可能会有问题

sqlite从哪里获得“14”绑定?我在空间字符上分裂,但它被忽略了,因为我做错了

用Bash做我的小项目会更容易吗

感谢所有花时间走到这一步的人。我有一个坏习惯,就是在美国的非主题领域需要帮助,在这里也需要帮助。我在使用tcl版本时有点挣扎,但现在它像冠军一样工作。我认为Python会有点直截了当。我仍然认为可能是这样

编辑:哇。一大堆新词被删掉了。对不起,我不知道怎么修理。我要去看看原始输入是否更好。

您将需要原始输入而不是脚本中的输入。输入计算您键入的内容,这就是为什么必须输入引号

您可以使用输入窗口上方的{}按钮标记代码。代码的实际降价是前4个空格

db.commit必须是db.commit

如果您这样做:

>>> import pythonmakenote
要运行mn,请执行以下操作:

>>> pythonmakenote.mn()
您还可以执行以下操作:

>>> from pythonmakenote import mn
>>> mn()
对于以下行:

db.execute('INSERT INTO note (note_txt) VALUES (?)', notetxt)
你需要:

db.execute('INSERT INTO note (note_txt) VALUES (?)', (notetxt,))
execute需要一个序列,因此如果传递一个字符串,它将充当单个字符的序列,因此您的14绑定错误是一个14个字符的字符串。xxx,是1元素元组的语法。将其列为清单[xxx]也会起作用

这是我最好的猜测。我没有您的数据库:

import sys
import sqlite3

def mn():
    conn = sqlite3.connect('data.db')
    db = conn.cursor()
    db.execute('select tag_text from tag')
    tagssofar = db.fetchall()
    print tagssofar

    print "Enter note text, remember to let console wrap long lines"
    notetxt = raw_input("note: ") 

    print "Enter 1 or more tags separated by spaces"
    taglist = raw_input("tags: ")
    taglist = taglist.split()

    db.execute('INSERT INTO note (note_txt) VALUES (?)', [notetxt])
    conn.commit()
    db.execute('select last_insert_rowid()')
    fknote = db.fetchone()[0]
    print fknote

    #records new tags since db trigger stops dups, updates many-many tbl

    for tagtxt in taglist: 
        db.execute('INSERT INTO tag VALUES (?)',[tagtxt])
        conn.commit()
        db.execute('select rowid from tag where tag_text = (?)',[tagtxt])
        fktag = db.fetchone()[0]
        print fktag
        db.execute('INSERT INTO fkeys VALUES (?,?)',[fknote,fktag])
        conn.commit()

这里发生了几件事。首先,mn不返回任何内容,您可能需要以下内容:

>>> def mn():
...     conn = sqlite3.connect('tester.db')
...     cur = conn.cursor()
...     return cur
... 
>>> c = mn()
这会留下一个开放的连接,因此当您使用c时,您将调用:

>>> c.connection.close()
此外,在游标上执行不会返回任何内容,您需要调用一些fetch方法,即fetchone或fetchall。将其中一些内容放在一起,我将开始修改如下:

import sys, tty
import sqlite3

def mn():
    conn = sqlite3.connect('/home/j...notes.sqlite')
    cur = conn.cursor()
    return cur

db = mn()

tags_so_far = db.execute('select tag_text from tag').fetchall()

print tags_so_far

print "Enter note text, remember to let console wrap long lines \n"
notetxt = raw_input("note: ")

print "Enter 1 or more tags separated by spaces \n"
taglist = raw_input("tags: ").split()

db.execute('INSERT INTO note (note_txt) VALUES (?)', notetxt)
db.commit()

fknote = db.execute('select last_insert_rowid()').fetchone()[0]

#records new tags since db trigger stops dups, updates many-many tbl

for tagtxt in taglist:
    db.execute('INSERT INTO tag VALUES (?)', (tagtxt,))
    db.commit()
    fktag = db.execute('select rowid from tag where tag_text = (?)',tagtxt)
    db.execute('INSERT INTO fkeys VALUES (?,?)',fknote,fktag)

谢谢Mark,完全解决了整个模块/名称问题。我将缩短模块名称,因为我必须每次键入它!即使使用原始输入、引号、甚至三重单引号,sqlite也会将notetxt的每个字母作为绑定进行计数。我认为对于变量传递有另一种表示法。去寻找它。再次感谢-杰德,你看到我最后一次编辑了吗?传递[notetxt]或notetxt,而不是notetxt。马克,我一定是太困了,没有注意到你在各个方面都完全解决了它!非常感谢你-杰桑克斯,bvmou。用fetch。。。它确实把标签打印到了屏幕上。参数替换根本不起作用,所以我正在使用字符串连接和sql注入进行原型化,或者使用文本进行原型化测试。我的数据库中的fetchone[1][0]将自动编号为rowid,因此我希望这是正确的下标。谢谢在我发现之前,请等待一段时间-即使是文字notetxt也会导致sqlite执行错误。在你和马克之间,我想我得到了足够好的激励。我希望我能“接受”你的两个答案。下个日落后我再查!哦,是的,至于mn返回任何东西:我想我可以让它返回一个成功的旗帜,但我不知道我会用它做什么。我想放一些试试/最后的东西吧。我的方法更多的是陈述/程序,而不是功能性的。我应该把这东西分解成小块吗
s、 你觉得呢?再次感谢马克和我。。。。fetchone的[0]下标中的Mark是正确的-我不必使用[1],因为我认为我必须根据自动编号rowid进行偏移。迫不及待地想完成其他4或5项功能-杰伊