python - 通过检查数据库并放置来解析python

标签 python mysql

我有一个表名= myclass,字段为:student

**student**
 1.Jane
 2.John
 3.Maria

我有一个文本文件“qwer.txt”,其中包含这些学生的详细信息,例如:

Jane is a good student and gets good marks.
john is a naughty one.
maria is a good student.

我有另一个数据库表“marks”,其中包含这些学生的分数:

 student english physics
 Jane     100     98 
 John     97      95
 Maria    90      91

现在,我需要的程序是我需要搜索数据库的名字“Jane”,并且必须搜索有关 Jane 的文本文件。如果找到 Jane,则必须打印该行,然后它必须连接秒 Marks 数据库并打印简英文标记。

我做了一半的编码

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
cursor = db.cursor()

    # execute SQL select statement
cursor.execute("SELECT name FROM myclass")


    # 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:
    for line in file:
        for key in keywords:
            if key in line:
               print line

它打印了该行,但缺少如何连接标记。请帮助

**MAIN OBJECTIVE**
It must search name one by one **from the table**(as input) and provide result to a text file.
It must connect another Marks database table and print marks to text file.

请理解我的疑问并帮助我!

最佳答案

一种方法:

 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
cursor = db.cursor()

    # execute SQL select statement
cursor.execute("SELECT name,english,pysics FROM myclass c,marks m where c.student=m.student")


    # commit your changes




rows=cursor.fetchall() 
 db.commit()




with open('qwer.txt','r') as file:
    for line in file:
        for row in rows:
            if rows[0] in line:
               print rows[0],'english mark',rows[1]

其他方式:

导入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
cursor = db.cursor()

    # execute SQL select statement
cursor.execute("SELECT name FROM myclass")


    # commit your changes




rows=cursor.fetchall() 
 )




with open('qwer.txt','r') as file:
    for line in file:
        for row in rows:
            if rows[0] in line:
               stmt="select english from marks where student = :student"
               cursor.execute(stmt,dict(student=row[0]))
               print "english marks:",cursor.fetchall()[0][0]

关于python - 通过检查数据库并放置来解析python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23955221/

相关文章:

mysql - 从预定义列表中选择 NOT IN

mysql - 如何确定在 MySQL 表中索引的内容

python - 在Python中使用Pillow时出现OSError : cannot identify image file I can't get rid off the Thumb. db

python - 如何改变 Pytorch 数据集的大小?

python - 如何在 Linux Ubuntu 12.04 上从 Python 调用脚本文件或可执行文件

python - 如何正确解码 Django HTML 模板中引用的可打印编码

mysql - VB.NET 如何通过检查数据库保存日期来创建提醒

php - 如何使用 mysqli 和 php(仅使用查询)基于另一列对两个表中的一列进行连接和求和

PHP 获取 python 进程并杀死它。 Xampp/Windows

php - 非常慢的mysql表