0

Attempting to execute a select query matching 4 variables, one of which is checking for a string in the title. How do I execute a LIKE statement with the python cursor class? or another way?

stmt = "SELECT * FROM publication NATURAL JOIN journal NATURAL JOIN authors WHERE name = ? AND year = ? AND booktitle = ? AND title LIKE '%%%s%%'" % (input_names, input_year, input_booktitle, input_string) 
print(stmt)
cur.execute(stmt)

Edit: table schemas

%%sql
DROP TABLE IF EXISTS publication;
CREATE TABLE publication(
    ID INT PRIMARY KEY NOT NULL,
    title VARCHAR NOT NULL
);

/* Author Entity set and writes_for relationship */
DROP TABLE IF EXISTS authors;
CREATE TABLE authors(
    name VARCHAR(200) PRIMARY KEY NOT NULL,
    ID INT,
    title VARCHAR(200),
    FOREIGN KEY(id, title) REFERENCES publication(ID, title)
);

/* Journal Entity set and apart_of relationship */
DROP TABLE IF EXISTS journal;
CREATE TABLE journal(
    booktitle VARCHAR(200) PRIMARY KEY NOT NULL,
    pages INT,
    year INT(4),
    ID INT,
    title VARCHAR(200),
    FOREIGN KEY(ID, title) REFERENCES publication(ID, title)
);
4
  • en.wikipedia.org/wiki/Where_(SQL)#LIKE Commented Apr 27, 2017 at 8:49
  • @jarlh I know of that syntax, doesn't work with this cur.execute() function Commented Apr 27, 2017 at 8:55
  • So LIKE isn't the issue? Commented Apr 27, 2017 at 8:55
  • @jarlh LIKE behaves differently when the cur.execute() function is working with it Commented Apr 27, 2017 at 8:57

1 Answer 1

1

I am thinking that the real problem is that you are doing join and you don't tell it from where to take the columns from(from which table).

can you please try to add the tables? I am not sure it will help but lets try(because maybe you don't have the same columns name in these tables)

like that?

stmt = "SELECT * FROM publication NATURAL JOIN journal NATURAL JOIN
authors WHERE authors.name = ? AND journal.year = ? AND
journal.booktitle = ? AND journal.title LIKE     
'%%%s%%'" % (input_names, input_year, input_booktitle, input_string) 
print(stmt)
cur.execute(stmt)

second thing: you should use '%s' and not '?'

stmt = "SELECT * FROM publication NATURAL JOIN journal NATURAL JOIN
authors WHERE authors.name = '%s' AND journal.year = '%s' AND
journal.booktitle = '%s' AND journal.title LIKE     
'%%%s%%'" % (input_names, input_year, input_booktitle, input_string) 
print(stmt)
cur.execute(stmt)
Sign up to request clarification or add additional context in comments.

5 Comments

Tried that block of code, and it did not work, I adjusted it a bit too, as title belongs to the journal table. I have attached my table schemas in an edit just now!
See my edit please(about the '?'). I see the schemas. please make sure you add to all columns their right tables too
Sorry added them after I made that comment haha
Boom fixed it, thank you! I will edit in the full correct block
Great:) You're Welcome

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.