【发布时间】:2021-11-21 00:21:25
【问题描述】:
"""
Created on Tue Sep 7 14:06:54 2021
@author: hp
"""
"""BOOK DETAILS IN SQL USING PYTHON----python librarian"""
import pandas as pd
import pyodbc
from sqlalchemy import create_engine,update
"""SERVER="DESKTOP-JKOITFK\SQLEXPRESS"
DATABASE="newdatabase"
DRIVER="SQL SERVER NATIVE CLIENT 11.0"
USERNAME="chitransh"
PASSWORD="reshushrey@2027"
#DATABASE_CONNECTION=f'mssql://{USERNAME}:{PASSWORD}@{SERVER}/{DATABASE}?driver={DRIVER}'"""
table_name="bookdetails"
engine=create_engine("mssql+pyodbc://@DESKTOP-JKOITFK\SQLEXPRESS/newdatabase?driver=SQL SERVER NATIVE CLIENT 11.0")
connection=engine.connect()
details={}
def bookdetails():
print("enter the book details:")
name=input("enter the name of the book:")
author=input("enter the author of the book:")
year=int(input("enter the year of the book:"))
publisher=input("enter the publisher of the book:")
quantities=int(input("enter the quantities of the book:"))
next_action=int(input("details entry completed. Press 1 for another entry, else press 2 for exit:"))
details={"BookName":[name],"Author":[author],"Year":[year],"Publisher":[publisher],"Quantities":[quantities]}
dict_df=pd.DataFrame(details)
#print(dict_df)
create_table=dict_df.to_sql(table_name,connection,if_exists="append",index=False)
if next_action==1:
bookdetails()
else:
authorized()
def issuebooks():
print("issue the books")
issue_book=input("which book to issue:")
frame=pd.read_sql("select Quantities from bookdetails where BookName = '{}'".format(issue_book),connection)
updated_value_query=(update(bookdetails).values(Quantities=(int(frame.values)-1)).where(bookdetails.BookName=='{}'.format(issue_book)))
connection.execute(updated_value_query)
def depositbooks():
print("deposit the books")
def authorized():
action=int(input("enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:"))
if action==1:
bookdetails()
elif action==2:
issuebooks()
elif action==3:
depositbooks()
#else:
# main()
def enter_func(username,password):
if username not in librarian.keys():
print("you are not authorized to enter")
else:
if password==librarian[username]:
print("enter")
authorized()
else:
print("password donot match,try again")
#main()
while True:
first=int(input("press 1 to login, 2 for exit:"))
if (first==1):
librarian={"deepika":"chiku","pragya":"praveen"}
username=input("enter the username:")
password=input("enter the password:")
enter_func(username,password)
else:
break
我正在尝试制作一个书籍输入系统,为此我正在尝试连接 SQL 和 python。当我尝试使用更新查询更新 SQL 中的值时,它会显示错误
press 1 to login, 2 for exit:1
enter the username:deepika
enter the password:chiku
enter
enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:2
issue the books
which book to issue:shiva2
Traceback (most recent call last):
File "<ipython-input-1-df831d64649f>", line 1, in <module>
runfile('C:/Users/hp/Desktop/project_part1.py', wdir='C:/Users/hp/Desktop')
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/hp/Desktop/project_part1.py", line 101, in <module>
enter_func(username,password)
File "C:/Users/hp/Desktop/project_part1.py", line 89, in enter_func
authorized()
File "C:/Users/hp/Desktop/project_part1.py", line 77, in authorized
issuebooks()
File "C:/Users/hp/Desktop/project_part1.py", line 61, in issuebooks
updated_value_query=update(bookdetails).values(Quantities=frame-1).where("BookName=='{}'".format(issue_book))
File "<string>", line 2, in update
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 735, in __init__
ValuesBase.__init__(self, table, values, prefixes)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 201, in __init__
self.table = _interpret_as_from(table)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\selectable.py", line 49, in _interpret_as_from
raise exc.ArgumentError("FROM expression expected")
ArgumentError: FROM expression expected
这是我面临的错误。它是说 FROM 表达式是预期的,但是当我在 SQL 中编写查询时,没有写入 FROM 表达式。我想更新 SQL 表中减去的值。
【问题讨论】:
标签: python sql-server python-3.x database sqlalchemy