简体   繁体   English

每次我运行python程序时如何添加新列

[英]how to add a new column every time i run a python program

i want my table to have 1st column as roll no,second as name.whenever i run python program i want to add a column of date in table.and in that new column i want to populate the list which i get from user.list will contain values 'P','A','P','P' etc.how to go about it? 我希望我的表的第一列为滚动号,第二个为名称。每当我运行python程序时,我想在表中添加一列日期。在该新列中,我想填充从user.list获取的列表会包含值'P','A','P','P'等如何处理? i tried first adding a column by alter command and then inserting data but nothing works. 我尝试先通过alter命令添加一列,然后插入数据,但是没有任何效果。

choice1=raw_input("\nEnter 'y' or 'n' to add new column:\n")
if choice1 is 'y':
    cursor.execute(" ALTER TABLE table1 ADD datecolumn varchar(20)")
    db.commit()
    for i in xrange(0,10):
        if students[i] is 'A':
            cursor.execute("insert into table1(datetime) values ('A')")
            db.commit()
        elif students[i] is 'P':
            cursor.execute("insert  into table1(datetime) values ('P')")
            db.commit()

Don't change your DB design at runtime, but design it in a way that you will change the data not the structure. 不要在运行时更改数据库设计,而要以一种可以更改数据而不是结构的方式进行设计。

You could have two tables. 您可以有两个表。 One called Student with columns rollno and name , maybe PK on rollno if it is unique. 一个名为Student的列,列rollnoname ,如果rollno是唯一的,则在PK上可能为PK。

Then have another table called Thing (any suitable name, but I don't know what your data is about) with three columns when (datetime), value (any suitable name) (CHAR(1)) and student (same type as rollno ). 然后,有一个叫做另一个表Thing (任何合适的名字,但我不知道你的数据是关于什么的),有三列when (日期时间), value (任何合适的名称)(CHAR(1))和student (同类型rollno )。

Define PK over both when and value to ensure that each student has only one value per date. whenvalue上定义PK,以确保每个学生每个日期只有一个价值。 Define a FK from Thing.student to Student.rollno . Thing.studentStudent.rollno定义FK。 Now your DB takes care of keeping your data (mostly) consistent. 现在,您的数据库将负责保持您的数据(大部分)保持一致。

Define indices depending on your needs of selects, inserts and updates over the different columns. 根据您对不同列的选择,插入和更新的需求定义索引。

Then for querying join both tables to get the desired result, eg 然后查询联接两个表以获得所需的结果,例如

select s.name, t.value
from Student s
left join Thing t on t.student = s.rollno
where t.when == 'whenever'

(I am not sure about the mysql dialect, so maybe some more quotes are needed. Please feel free to edit.) (我不确定mysql的方言,因此可能需要更多的引号。请随时进行编辑。)

暂无
暂无

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

相关问题 每次运行程序时如何让 Python 在 excel 中添加新行? - How to get Python to add a new row in excel every time I run the program? 每次运行程序时,都没有正确显示Python Curses窗口 - Python Curses windows are not shown properly every time I run the program 如何在每次下载文件时自动运行python程序? - How to make python program run automatically every time I download a file? 如何在每次重新运行程序时停止openpyxl-python清除我的excel文件? - How to stop openpyxl - python from clearing my excel file every time I re-run the program? 每次在Python中运行代码时,如何以相同的顺序随机化pandas列? - How do I randomize a pandas column in the same order every time I run the code in Python? 每次使用Python运行新的文本文件 - New textfile every time run with Python 每次运行 python 程序时,执行时间都不同。 有什么办法可以解决这个问题吗? - Execution time is varying every time I run the python program. Is there any way to solve this? Python Selenium | 每次添加时如何删除这些重复代码让我们说一个新的“项目” - Python Selenium | How can I remove these repetitive code every time I add let's say a new "item" 为什么Eclipse每次尝试运行我的Python程序时都会要求'ant build'? - Why does Eclipse ask me to 'ant build' every time I try to run my Python program? Django python - 每次更新屏幕时如何添加新记录? 相反,它会更新现有记录。? - Django python - how do add new record every time I update the screen? instead it updates the existing record.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM