This is a more flexible way of seeding data 1. Create required directories and files $ mkdir testapp/management $ mkdir testapp/management/commands $ touch testapp/management/__init__.py $ touch testapp/management/commands/__init__.py 2. Write code that populates the db $ vi testapp/management/commands/populate_db.py from django.core.management.base import BaseCommand from testapp.models import Region from django.db import connection class Command(BaseCommand): args = '' help = 'Management script that populates the database with initial seed' def _truncate_region(self): cursor = connection.cursor() cursor.execute("TRUNCATE TABLE `testapp_region`") def _create_regions(self): regions = ( ('REG001', [ {"name": "vmk-4027","vlanId":4027,"usefor":["vsan","nfs"]}, {"name": "vmk-4028","vlanId":4028,"usefor":["traffic"]}, {"name": "vmk-102","vlanId":102,"usefor":["pub"]}, ]), ('REG002', [ {"name": "vmk-4029","vlanId":4029,"usefor":["vsan","nfs"]}, {"name": "vmk-4030","vlanId":4030,"usefor":["traffic"]}, {"name": "vmk-102","vlanId":103,"usefor":["pub"]}, ]), ) for item in regions: region = Region(name=item[0], vlans=item[1]) region.save() def handle(self, *args, **options): self._truncate_region() self._create_regions() 3. Execute the program $ python manage.py populate_db
Showing posts with label database. Show all posts
Showing posts with label database. Show all posts
7.26.2017
Django Data Seeding
12.16.2013
Rails 4 Connect Existing Database
Once connect strings are specified in database.yml file, just add a model as follows:
class TestResult < ActiveRecord::Base self.table_name = "Test" self.primary_key = "testId" endNow, lets check from the console
$ rails c Loading development environment (Rails 4.0.2) 2.0.0p353 :002 > TestResult.column_names => ["testId", "analysis", "archive", "baseline", "description", "engineer", "name", "project", "reference", "retention", "link", "errors"]
11.22.2013
Sqlite Cheat Sheet
This is from command line shell for sqlite.
1. Open sqlite database
1. Open sqlite database
$ sqlite3 db/development.sqlite32. List down tables
sqlite> .tables3. Look at the schema
sqlite> .schema4. Exit
sqlite> .exit
9.12.2013
MySQL Cheat Sheet
1. Connect to MySQL server in localhost
>mysql -u username -p password [databasename]2. Executing SQL Statements from a Text File
mysql> source c:\filename.sql3. Migrate database
# everything $ mysqldump -u [user] -p --opt mydb > mydb.sql # To export to file (data only) mysqldump -u [user] -p --no-create-info mydb > mydb.sql # To export to file (structure only) mysqldump -u [user] -p --no-data mydb > mydb.sql # To import to database mysql -u [user] -p mydb < mydb.sql
Subscribe to:
Posts (Atom)