我目前正在开发一个通过Jython2.7运行的Python脚本,我正在使用Jython的sqlite3模块(link),它的语法与CPython sqlite3模块相同。
代码如下:
def file_exists(self, cur, file_hash):
cur.execute("SELECT 1 FROM samples WHERE file_hash = ?", (file_hash,))
ret = cur.fetchone()
if ret is None:
return False
return True
cur.execute("SELECT 1 FROM samples WHERE file_hash = ?", (file_hash,))
TypeError: execute() takes exactly 2 arguments (3 given)
我在互联网上看了看,语法似乎是正确的,所以我不明白是什么导致了这个问题。
编辑:
import os
import sys
import time
import json
import logging
import sqlite3
from hashlib import sha1
ANALYSIS_FAILED = 0
ANALYSIS_SUCCESS = 1
ANALYSIS_ALREADY = 2
class ProgramAnalyzer:
def __init__(self):
self.filename = None
self.cyclomatic_complexities = None
self.primes = None
self.signature = None
self.file_hash = None
self.total_functions = 0
self.analysis_date = None
self.architecture = None
def file_exists(self, cur, file_hash):
cur.execute("SELECT 1 FROM samples WHERE file_hash = ?", (file_hash,))
ret = cur.fetchone()
if ret is None:
return False
return True
def analyze(self):
t = time.time()
executable_path = getCurrentProgram().getExecutablePath()
content = open(executable_path, 'r').read()
self.file_hash = sha1(content).hexdigest()
# Checking whether such file is already in the database
conn = cur = None
try:
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
except sqlite3.Error as e:
logging.error("Database error: " + str(e))
return ANALYSIS_FAILED
if self.file_exists(cur, self.file_hash):
return ANALYSIS_ALREADY
print("File not in database")
[initializing remaining object fields]
return ANALYSIS_SUCCESS
def main():
analyzer = ProgramAnalyzer()
ret = analyzer.analyze()
if ret == ANALYSIS_FAILED:
sys.exit(1)
if __name__ == "__main__":
main()
像getCurrentProgram()和getExecutablePath()这样的函数就是我正在使用的Java API。
发布于 2019-04-09 03:45:09
这似乎是jython-sqlite3的一个问题。它对Cursor.execute()
有一个不同的签名:https://github.com/anthonyhendrickson/jython-sqlite3/blob/master/sqlite3/module.py#L27
也就是说,不同于Python标准库中相同函数的签名,后者支持SQL参数:https://docs.python.org/2/library/sqlite3.html#sqlite3.Cursor.execute
https://stackoverflow.com/questions/55580209
复制相似问题