在mysql中用伪数据自动填充表 [英] automatically populate table with dummy data in mysql

查看:86
本文介绍了在mysql中用伪数据自动填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MySQL表,我想用一些虚拟数据填充该表进行测试(超过50个).

I have a MySQL table which I want to populate with some dummy data for testing (50+).

此表具有到另一个表的外键,因此虚拟数据必须从该表交叉引用,但又是随机的,即不能全部都是相同的外键.

This table has a foreign key to another table so the dummy data must cross reference from that table but again be random i.e. can't all be the same foreign key.

它也有一个添加日期的字段,我想在一年的跨度内填充一个随机日期,例如在2010年的任何日期.

It also has a date added field which I want to populate with a random date within a year span e.g. any date in the year 2010.

我的表结构是:

id, customer_id, date_added, title, total_cost

其中id是主键,customer_id是外键,date_added是日期字段.

where id is the primary key, customer_id is the foreign key and date_added is the date field.

做到这一点的最佳方法是什么?我宁愿直接在MySQL中执行此操作,但如果我的站点不是在Python上运行,则可以这样做.

What is the best way of doing this? I'd prefer to do it directly in MySQL but if not my site is running on Python so a way of doing this in that would do.

推荐答案

如果您真的想放弃一些测试数据的设置,则应该走夹具路线.这将帮助您建立一个非常漂亮的开发环境,并且如果您正在使用它,则可以很好地集成到您网站的框架中.

If you really want to get down with some setting up of testing data, you should go the fixture route. This will help set yourself up a pretty nice development environment and may integrate very nicely into your website's framework if you're using one.

您可以在此处

如果您认为要完成所有工作还有些繁琐,请查看MySQLdb模块,该模块将帮助您将数据插入表中.

If you think that's a little too much work to get all working, look into the MySQLdb module which will help you insert data into your table.

链接回stackoverflow可能不太好,但是有人已经回答了您要询问的日期问题.您可以在此处.

It may be in poor taste to link back to a stackoverflow, but someone has already answered the date question you are asking. You can find that here.

这篇关于在mysql中用伪数据自动填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆