我正在将一个tcl脚本移植到python,因为我似乎无法在我的诺基亚N810上获得tclSqlite。脚本提示输入,将它们传递给带有3个表的sqlite数据库:Notes,Tags和NotesXTags多对多tbl。有一些触发器可以阻止我多次存储任何标签。作为一个菜鸟/业余爱好者,我一行一行地通过tcl脚本替换每个Python线。不是非常Pythonic,但我是一个业余爱好者,在我让这个脚本在N810上工作后无意使用该语言。我确实看过每一个Q& A S.O.建议我和我一直在努力工作几个小时。我至少有3个无知的错误。一个名为'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
所以我做'导入pythonmakenote'。到现在为止还挺好。我输入“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.有关代码块标记和其他降价的说明吗?
为什么我不能在模块中定义并使用它? Python: NameError: global name 'foobar' is not defined是否相关? (对我的问题)
我还有其他几个Defs用于通过标签获取笔记,获取标签列表等等。我认为它们都可以放在一个模块中。
我不想在输入(注释或空格分隔的标签列表)周围加上引号。那可行吗?我有进口tty的东西,但我没有使用它(不知道怎么样,但我开始怀疑我将不得不学习)
如果我在提示时放入3个标签,没有引号,我会收到'意外的EOF'错误。为什么呢?
我看到字符串是不可变的,所以可能将列表/拆分分配给一个字符串var之前可能会出现问题?
sqlite在哪里获得'14'绑定?我正在拆分空间字符但是它被忽略了(因为我做错了)?
在Bash中完成我的小项目会更容易吗?
感谢任何花时间去做这件事的人。我有一个坏习惯,需要在S.U.的专题领域寻求帮助。这里也是noob-RTFM。我在tcl版本上有点挣扎,但它现在就像一个冠军。我希望Python有点简单。仍然认为可能是。
编辑:哇。一堆新线被剥离了。对不起,我不知道如何解决。我要看看“raw_input”是否效果更好。
答案 0 :(得分:2)
您的脚本中需要raw_input
而不是input
。 input
评估您输入的内容,这就是您必须输入引号的原因。
您可以使用输入窗口上方的{}
按钮标记代码。代码的实际降价是前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 bindings
错误(它是一个由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()
答案 1 :(得分:1)
这里有几件事情要发生。首先,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)