【发布时间】:2014-07-01 22:48:17
【问题描述】:
我知道要连接到一个数据库并连接它,我是通过
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
我有一个文本文件 qwer.txt 我有表名:vehicle
我需要执行打印与文本文件和数据库匹配的单词的操作。我对小数据库和小文件的操作是由此给出的,但是连接数据库并匹配它们呢?
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
输出:汽车
那么,直接连接数据库和执行匹配操作的文件名需要做哪些更改。
【问题讨论】:
-
你有关键词的表名
-
vehicle 是表名
-
你想知道如何从表中检索值并正确搜索吗?
-
当然可以,请帮忙。