I know to connect to a db and connected it and i done it by
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="mysql", # your password
db="sakila") # name of the data base
I have a text file qwer.txt i have the table name:vehicle
I need to perform the operation which is to print words that match the text file and database.my operation for a small db and small file is given by this but what about connecting the database and matching them?
mysql_row = "car,bike"
file_str = "I have a car"
mysql_elements = mysql_row.split(",")
file_elements = file_str.split(" ")
output_array = []
for m_element in mysql_elements:
for i in range(len(file_elements)):
if ((m_element == file_elements[i]) and (i < len(file_elements)-1)):
output_array.append(file_elements[i])
print output_array
Output:car
So,what are the changes to be done for connecting the database directly and the filename to perform the matching operation.
cursor = db.cursor()
# execute SQL select statement
cursor.execute("SELECT name FROM Vehicle")
# commit your changes
db.commit()
keywords=[]
here fetchall() gets all the rows and we append carnames to key words
for i in cursor.fetchall():
keywords.append(i[0])
with open('qwer.txt','r') as file:
this is used to open a file
for line in file:
for key in keywords:
if key in line:
print line
for each word in keywords search the line brings our output
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.