我正在尝试做一个小程序,使用 Genie 编程语言搜索 SQLite 数据库中的特定条目。但是,我陷入了准备好的语句没有选择正确的条目的地步。我想该语句的语法有问题。
首先,这里是搜索功能:
def SearchForRecipe (db:Database)
print "-------------------------------"
print " Search in"
print "-------------------------------"
print " 1 - Recipe Name"
print " 2 - Recipe Source"
print " 3 - Ingredients"
print " 4 - Exit"
searchin:string = UserInterface.raw_input("Enter Search Type -> ")
search:string = " "
sql:string = " "
response:string=" "
if searchin != "4"
if searchin == "1"
search = "Recipe Name"
else if searchin is "2"
search = "Recipe Source"
else if searchin is "3"
search = "Ingredients"
else
print "An Error Occured"
print "--------------------------------------------------------------------------------------"
Process.exit (-1)
parm:string = searchin
response = UserInterface.raw_input ("Search for what in "+ search +" (blank to exit) -> ")
if parm == "1" // Recipe Name
stmt:Statement = PreparedStatements.select_recipe(db, response)
var row = new dict of string, string
cols:int = stmt.column_count ()
print cols.to_string()
for i:int = 0 to (cols - 1)
row[ stmt.column_name( i ) ] = stmt.column_text( i )
stdout.printf( "%-30s", row[ "name" ])
stdout.printf( "%-20s", row[ "servings" ])
stdout.printf( "%-30s\n", row[ "source" ])
问题发生在 if 循环中:if parm == "1" // Recipe Name
在下一行中,当我要求打印所选条目的列数时,它返回零。我添加了该行以尝试解决正在发生的事情,但这可能不是最好的方法。
准备好的语句位于命名空间内,如下所示:
namespace PreparedStatements
def select_recipe (db:Database, res:string) : Statement
statement:Statement
db.prepare_v2( "SELECT pkid,name,source,servings FROM Recipes WHERE name like" + res, -1, out statement)
return statement
我想 select_recipe 函数中的 SELECT 语句有问题,因为它没有在数据库中选择正确的条目。
问题:如何正确选择条目?我应该使用另一种方法,还是准备好的语句是正确的方法?