0

I am using pd.read_sql_query to take data from sqlite to pandas. No surprises but the below works as expected.

import sqlite3
import pandas as pd

conn = sqlite3.connect('AmApp.db')
sql = """SELECT * FROM clients WHERE first_name='john'""", conn)
df = pd.read_sql_query(sql, conn)
print (df)

However as soon as I add a variable into first name, as below, I get the error:

pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT * FROM clients WHERE first_name=john': no such column: john

import sqlite3
import pandas as pd

conn = sqlite3.connect('AmApp.db')
sql = """SELECT * FROM clients WHERE first_name={}""".format('john'))
df = pd.read_sql_query(sql, conn)
print (df

I do not quite understand where i am going wrong with this.

1
  • 3
    TRY THIS sql = """SELECT * FROM clients WHERE first_name='{}'""".format('john')) Commented Aug 2, 2019 at 10:54

1 Answer 1

2

You need to put quotes around the value:

sql = """ SELECT * FROM clients WHERE first_name='{}' """.format('john'))
Sign up to request clarification or add additional context in comments.

Comments

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.