python - Connecting multiple columns of a table -
- i writing code taking data multiple columns of
mysql
database. - in coding need print lines present in database, did , works fine it's not printing next column word match.
- i have text file
"qwer.txt"
containsline1:"i have car".line2:"i have phone"
- my
tablename
'adarsh1'
in"a1"
column consists of car ,"a2"
consists of phone.
according coding prints "i have car"
instead of "i have car"
and "i have phone"
.
so, problem in coding preventing me printing both line since presents in both column of table?
import mysqldb db = mysqldb.connect(host="localhost", # host, localhost user="root", # username passwd="mysql", # password db="sakila") # name of data base cursor = db.cursor() # execute sql select statement cursor.execute("select a1,a2 adarsh1") # commit changes db.commit() keywords=[] #here fetchall() gets rows , append carnames key words in cursor.fetchall(): keywords.append(i[1]) open('qwer.txt','r') file: line in file: key in keywords: if key in line: print line
you append 1 column keyword. should append both i[0] , i[1]:
import mysqldb db = mysqldb.connect(host="localhost", # host, localhost user="root", # username passwd="mysql", # password db="sakila") # name of data base cursor = db.cursor() # execute sql select statement cursor.execute("select a1,a2 adarsh1") # commit changes db.commit() keywords=[] #here fetchall() gets rows , append carnames key words in cursor.fetchall(): keywords.append(i[0]) keywords.append(i[1]) open('qwer.txt','r') file: line in file: key in keywords: if key in line: print line
Comments
Post a Comment