(Jun-16-2020, 03:17 AM)card51shor Wrote: [ -> ]Your example is not taking user input so it doesn't really help me as far as syntax goes.
Then add user input to it

, try things for yourself !!!
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
class Book(Base):
__tablename__ = 'book'
id = Column(Integer, primary_key=True)
name = Column(String)
author = Column(String)
def __repr__(self):
return f'<Book(name= {self.name}, author= {self.author})>'
Base.metadata.create_all(engine)
book = Book(name='Coding', author='Yoriz')
session.add(book)
book2 = Book(name='More Coding', author='some one')
session.add(book2)
session.commit()
print(session.query(Book).all())
# search_string = '%or%' # % before and after match anything
search_string = input('Input the search string (note wild cards are added here )')
print(session.query(Book).filter(Book.author.like(search_string)).one())
print(session.execute('SELECT * FROM book WHERE author LIKE :author',
{'author': search_string}).fetchone())
if you only enter 'or'
you will get the error
Error:
sqlalchemy.orm.exc.NoResultFound: No row was found for one()
using wild cards will find a result
Output:
<Book(name= Coding, author= Yoriz)>
(1, 'Coding', 'Yoriz')
(Jun-16-2020, 03:17 AM)card51shor Wrote: [ -> ]Please don't just send me the link to the documentation - it isn't specific and it's very confusing.
Learn from documentation !!!
SQL LIKE Operator
(Jun-16-2020, 03:17 AM)card51shor Wrote: [ -> ]Where can I see where I can just learn how to type the SQL code? I don't get it. It shouldn't be this complicated. It's easier to write actual SQL code on its own. Why would Python make it more complicated? With all these placeholders and secret curly brackets and all this?
Sqlalchemy makes it easier why not leverage it go through the tutorial
Object Relational Tutorial
(Jun-16-2020, 03:17 AM)card51shor Wrote: [ -> ]Here is my code currently :
@app.route("/search", methods=["GET", "POST"])
def search():
searchString = 'user'
b = db.execute("SELECT * FROM books WHERE author LIKE :search", {'search': searchString}).fetchone()
print(b)
return render_template("search.html")
Are you making use of wildcards if you are not adding them nothing will be found if you are searching for a part of a sting
what happens if you add the wildcards in like below
@app.route("/search", methods=["GET", "POST"])
def search():
searchString = '%user%'
b = db.execute("SELECT * FROM books WHERE author LIKE :search", {'search': searchString}).fetchone()
print(b)
return render_template("search.html")
if you want to add the wild cards automatically, add them in your method
@app.route("/search", methods=["GET", "POST"])
def search():
search = f'%{search}%'
b = db.execute("SELECT * FROM books WHERE author LIKE :search", {'search': search}).fetchone()
print(b)
return render_template("search.html")