简体   繁体   English

我应该为MySQL数据库插入程序使用python类吗?

[英]Should I use python classes for a MySQL database insert program?

I have created a database to store NGS sequencing results. 我创建了一个数据库来存储NGS测序结果。 It consists of 17 tables to store all of the information. 它由17个表组成,用于存储所有信息。 The results are stored in spreadsheets which I parse data from and store in variables using python (2.7), and then use the python package mysqldb to insert data into the database. 结果存储在电子表格中,我使用python(2.7)解析数据并将其存储在变量中,然后使用python包mysqldb将数据插入数据库中。 I mainly use functions to obtain the information i need in variables, then write a loop in which I call this function followed by a 'try:' statement to insert. 我主要使用函数来获取变量中所需的信息,然后编写一个循环,在其中调用此函数,然后插入“ try:”语句。 Here is a simple example: 这是一个简单的示例:

def sample_processer(file):
    my_file = open(file, 'r+')
    samples = []
    for line in my_file:
        ...get info...
        samples.append(line[0])
    return(samples)

samples = sample_processor('path/to/file')

for sample in samples:
    try:
        sql = "samsql = "INSERT IGNORE INTO sample(sample_id, diagnosis, screening) VALUES ("
        samsql = samsql + "'"+sample+"'," +sam_screen_dict.get(sample)+"')"  
    except e: 
        db.rollback()
        print("Something went wrong inserting data into the sample table: %s" %(e))

*sam_screen_dict is a dictionary i made from another function. * sam_screen_dict是我从另一个函数制作的字典。

This is a simple table that I upload into but many of them call of different dictionaries to make sure the correct results are uploaded. 这是我上载的简单表格,但其中许多表调用了不同的字典,以确保上载了正确的结果。 However I was wondering whether there would be a more robust way in which to do this using a class. 但是我想知道是否会有使用类的更健壮方法。

For example, my sample_id has an associated screening attribute in the sample table, so this is easy to do with one dictionary. 例如,我的s​​ample_id在样本表中具有关联的筛选属性,因此使用一本字典就很容易做到。 I have more complex junction tables, such as the table in which the sample_id, experiment_id and found mutation are stored, alongside other data, would it be a good idea to create a class for this table, calling on a simple 'sample' class to inherit from? 我有更复杂的联结表,例如其中存储了sample_id,experiment_id和发现的突变的表以及其他数据,为该表创建一个类,并调用一个简单的“ sample”类来创建一个类是一个好主意吗?继承? That way I would always know that the results being inserted will be for the correct sample/experiment etc. 这样,我将永远知道插入的结果将用于正确的样本/实验等。

Also, using classes could I write rules for each attribute so that if the source spreadsheet is for some reason incorrect, it will not insert into the database? 另外,可以使用类为每个属性编写规则,以便如果源电子表格由于某种原因不正确,它将不会插入数据库中? Ie: sample_id is in the format A123/16. 即:sample_id的格式为A123 / 16。 Therefore using a class it will check that the first character is 'A' and that sample_id[-3] should always == '/'. 因此,使用类将检查第一个字符为“ A”,并且sample_id [-3]应始终=='/'。 I know I could write these into functions, but I feel like it would take up so much space and time writing so many 'if' statements, that if it is stored once in a class then this would be alot better. 我知道我可以将它们写到函数中,但是我觉得它会占用大量空间和时间来编写许多“ if”语句,因此如果将其存储在类中一次,那会更好。

Has anybody done anything similar using classes to pass through their variables to test that they are correct before it gets to the insert stage and an error is created? 有没有人做过类似的事情,使用类来传递变量以测试它们是否正确,然后再进入插入阶段并创建错误?

I am new to python classes and understand the basics, still trying to get to grips with them so a point in the right direction would be great - as would any help on how to go about actually writing the code for a python class that would be used to make a more robust database insertion program. 我是python类的新手,并且了解基本知识,但仍在尝试与之接触,因此朝着正确的方向发展将是一件很不错的事-有助于实际编写python类代码的任何帮助用于制作更强大的数据库插入程序。

17tables it means you may use about 17 classes. 17个表,这意味着您可以使用大约17个类。 Use a simple script. 使用一个简单的脚本。 webpy.db https://github.com/webpy/webpy/blob/master/web/db.py just modify few code. webpy.db https://github.com/webpy/webpy/blob/master/web/db.py只需修改一些代码。 Then you can use webpy api: http://webpy.org/docs/0.3/api#web.db to finish your job. 然后,您可以使用webpy api: http ://webpy.org/docs/0.3/api#web.db完成您的工作。

Hope it's useful for you 希望对您有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM