[英]How do I create a function that will return a value in a dictionary for each row within a data sheet using Python?
[英]How do I return all row values of a data value I put as an argument into a search function when using postgresql with python
import psycopg2
def connect_table():
connect= psycopg2.connect("dbname= 'bookshop' user='postgres' password='0973' host='localhost' port='5432'")
cursor= connect.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS books (ID SERIAL PRIMARY KEY, Title TEXT, Author TEXT, Year INTEGER, ISBN INTEGER UNIQUE)")
connect.commit()
connect.close()
def insert(Title,Author,Year,ISBN):
connect= psycopg2.connect("dbname= 'bookshop' user='postgres' password='0973' host='localhost' port='5432'")
cursor= connect.cursor()
cursor.execute("INSERT INTO books (Title, Author, Year, ISBN) VALUES (%s,%s,%s,%s)", (Title,Author,Year,ISBN))
connect.commit()
connect.close()
def view():
connect= psycopg2.connect("dbname= 'bookshop' user='postgres' password='0973' host='localhost' port='5432'")
cursor= connect.cursor()
cursor.execute("SELECT * from books")
rows= cursor.fetchall() #Fetches all rows of a query result, returns it as a list of tuples
connect.close()
return rows
def search(Title='',Author='',Year='',ISBN=''):
connect= psycopg2.connect("dbname= 'bookshop' user='postgres' password='0973' host='localhost' port='5432'")
cursor= connect.cursor()
cursor.execute("SELECT * from books WHERE Title=%s OR Author=%s OR Year=%s OR ISBN=%s", (Title, Author, Year, ISBN))
rows= cursor.fetchall() #Fetches all rows of a query result, returns it as a list of tuples
connect.close()
return rows
print(search(Author= 'Tsu Chi'))
上面是我的代码,下面是我以这种方式调用搜索函数时遇到的错误print(search(Author= 'Tsu Chi'))
。 我希望能够搜索单个数据值,它会返回与之关联的行...
Traceback (most recent call last):
File "c:/Users/daniel/Desktop/backend.py", line 77, in <module>
print(search(Author= 'Tsu Chi'))
File "c:/Users/daniel/Desktop/backend.py", line 36, in search
cursor.execute("SELECT * from books WHERE Title=%s OR Author=%s OR Year=%s OR ISBN=%s", (Title, Author, Year, ISBN))
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: ""
LINE 1: ... books WHERE Title='' OR Author='Tsu Chi' OR Year='' OR ISBN...
似乎至少有一个字段(标题、作者、年份或 isbn)是数据类型int
。 您为函数参数定义的默认值导致 psycopg2 将字符串值传递给所有绑定变量。
附带说明一下,通常最好保持一个连接打开并将其重用于所有函数调用。
psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: ""
LINE 1: ... books WHERE Title='' OR Author='Tsu Chi' OR Year='' OR ISBN..
此错误是因为 'Year' 和 'ISBN' 是整数类型,但您将空字符串传递给它们。
如果您想根据单个或多个条件进行搜索,请尝试使用如下代码所示的可变长度关键字参数:
我是一个 noob 编码员,所以我不知道这是否是最优雅或最有效的代码,但它应该可以达到目的。
您可以在“where”子句中使用单个或多个条件进行搜索,并传递所需的条件 joiner(or/and)。
def search(condition_joiner='',**kwargs):
connect= psycopg2.connect("dbname= 'bookshop' user='postgres' password='0973' host='localhost' port='5432'")
cursor= connect.cursor()
search_string='SELECT * from books WHERE '
condition_count=0
for key,value in kwargs.items():
condition_count+=1
if condition_count>1:
search_string += condition_joiner
search_key= ' {}={}'.format(key,value)
search_string = search_string+search_key
cursor.execute(search_string)
rows= cursor.fetchall() #Fetches all rows of a query result, returns it as a list of tuples
connect.close()
return rows
print(search(Author= '\'Tsu Chi\''))
使用变量search_string
将语句传递给cursor.execute()
search_key
是我们放置所有必需条件(1 个或多个)并将其一一附加到search_string
的地方。
如果您有 1 个以上的条件condition_joiner
用于连接不同的条件,例如Author='some_author' OR Title='some_title'
您可以在search()
传递任意数量的参数。 确保在传递字符串时使用转义字符 \\ 来使用引号。
如果有效,请更新。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.