I am trying to create a function that will query a database using a string (or part of a string) inserted via raw_input.
The database PManDB
has only one table ( accounts
) with 3 columns ( website, username, password
).
Example: if in the column website
I have the string 'www.website.com'
I want to obtain it inserting the values 'w'
or 'web'
or 'com'
in the raw_input
.
The full code of the project is here.
Below an extract without the function to print the database:
def SearchRecords(self):
search = raw_input("Insert value to be searched in the DB:")
db = MySQLdb.connect("localhost", "root", "pass", "PManDB")
cursor = db.cursor()
sql = """SELECT * FROM accounts WHERE website LIKE %s"""
data = (search)
try:
# Execute the SQL command
cursor.execute(sql, data)
print "The research has been made with success"
except:
# Rollback in case there is any error
print "There is an error"
db.rollback()
# disconnect from server
The idea is to add a cursor.fetchall()
after the print
statement "The research has been made with success"
using the following sintax
result = cursor.fetchall()
print result
Or to add a for
loop
result = cursor.fetchall()
for row in result:
print row[0]
But in both ways the code is not working.
Could you please help me understanding where am I wrong and why?
EDIT1: According to mata comment, I have added the error.
When I launch the application in PyCharm, introducing the modification proposed by mata, I receive the following error
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
The code updated is the following (still not working with the error above):
def SearchRecords(self):
search = raw_input("Insert value to be searched in the DB:")
db = MySQLdb.connect("localhost", "root", "studio", "PManDB")
cursor = db.cursor()
sql = """SELECT * FROM accounts WHERE website LIKE %s"""
data = ('%' + search + '%',)
cursor.execute(sql, data)
result = cursor.fetchall()
print result
Solved removing the db.close and the db.commit at the end of the function.