When you use the feature "generate database from model" how do you put some data into a table?
for example i want a tabel with all country after the database is created.
Is there a script / shortcut to do this?
You have following options
write a program to create db/table and
insert data
create SQL script to create db/table and
insert data
use the database management tool
(e.g. Management Studio for SQL
Server) to create db/table and insert
data
Are you looking for the Seed() method?
Accepted Ajay_Whiz answer since it helped me the most i created a bat file that just executes the contex.sql and then some other *.sql files to populate the database.
Disclaimer this is for development database do not ever ever use this on you productions database. unless you know what your doing
The generated contex.edmx.sql by Entity framework drops all your data!!!
::Automated databasecreater
#echo off
echo createing database
SQLCMD -S. -E -iContext.edmx.sql -e -b
echo add gemeentes
SQLCMD -S. -E -iContainsDataOfTableX.sql -b
pause
Related
I'm trying to script the database objects and data of my database to later move it to a server where I don't have backup/restore rights. Instead I'm using the Generate Scripts method and I use mssql-scripter to generate the scripts.
I have a .bat file with the following script code to generate my SQL script file.
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql
--schema-and-data --script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --include-dependencies --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database
The problem is that it's also scripting DROP DATABASE and CREATE DATABASE, which I don't want. I would only like to DROP and CREATE database objects and later populate tables with the scripted data.
Has anyone faced this problem and have you found a solution?
After fiddling around with the options for longer, I managed to find the right parameter and work-around to solve my problem.
The exact code that I ran is:
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql --schema-and-data
--script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database --include-objects "dbo." --display-progress
The key change I added the --include-objects parameter, with a twist. The way I changed by scripts is by adding code snippet:
--include-objects "dbo."
This tells mssql-scripter to only script out objects that contain the "dbo." keyword(substring) in the fully qualified name.
Also I remove this parameter from my initial command:
--include-dependencies
since I script out everything in my database under the dbo schema.
This scripts out:
all of the objects in my database
it includes a IF EXISTS check
it issues a DROP query to drop the existing
it issues a CREATE query to create the new one
it issues multiple INSERT statements to also populate the database with data
I have made changes to my model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:
syncdb: only works first time, not with changes
"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).
Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.
Suggestions?
First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do
sudo apt-get install sqlite3
On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.
Use it like this:
> sqlite3 /path/to/your/database
;show some help
.help
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;
See also the command line reference: http://www.sqlite.org/sqlite.html
and the sqlite SQL reference: http://www.sqlite.org/lang.html.
Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema in sqlite3, and the output of manage.py sql my_site to find out which columns you need to add.
An example:
ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text
Use Django's built in database management tool:
python manage.py dbshell
And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).
I want to automatically (ideally from the command prompt in a batch file) automate the generation of the schema of my SQL Server 2008 R2 database.
In SSMS, I can right-click the DB, choose "Tasks", "Generate scripts", and then follow the wizard to gen a Schema script. Is there a command-line version of this process that I can use?
Microsoft released a new tool a few weeks ago called mssql-scripter that's the command line version of the "Generate Scripts" wizard in SSMS. It's a Python-based, open source command line tool and you can find the official announcement here. Essentially, the scripter allows you to generate a T-SQL script for your database/database object as a .sql file. You can generate the file and then execute it. This might be a nice solution for you to generate the schema of your db (schema is the default option). Here's a quick usage example to get you started:
$ pip install mssql-scripter
# script the database schema and data piped to a file.
$ mssql-scripter -S localhost -d AdventureWorks -U sa > ./adventureworks.sql
More usage examples are on our GitHub page here: https://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md
From this answer, there appear to be tools called SMOScript and ScriptDB that can do that.
If you find a way without third party tools please share :)
I am looking for a command line program that extracts a script containing commands for creations of all database objects (tables, views, procedures, generators, types). It should work with MySQL, Firebird, Oracle and MS SQL Server.
As a part of our automatic build process we would like to put in our SVN repository a script for database creation in an automatic fashion. Firebird comes with a tool that do what we want but we prefer a generic one so we can include as an ANT task.
For SQL Server you can use the Database Publishing Wizard
I can't help you with the other DB's, however. Good Luck.
To Script the Schema and Data
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql
and to just script the Schema
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql -schema
Is it possible to generate sql scripts for my sql server 2005/8 database via the command line?
I want to automate the build process and part of it requires me to get the entire schema (tables/sprocs/etc) in a file.
Microsoft released a new tool last week called mssql-scripter. The tool is a Python-based, open source command line tool and you can find the official announcement here. Essentially, the scripter allows you to generate T-SQL scripts (DDL and DML) for your database/database object as a .sql file. It's the command line version of the 'Generate Scripts' wizard in SSMS. Here's a quick usage example to get you started (schema only is the default):
$ pip install mssql-scripter
# script the database schema and data piped to a file.
$ mssql-scripter -S localhost -d AdventureWorks -U sa > ./adventureworks.sql
More usage examples are on our GitHub page here: https://github.com/Microsoft/sql-xplat-cli/blob/dev/doc/usage_guide.md
There used to be an utility called SCPTXFR.EXE which was part of the standard SQL Server 7.0 and 2000 install.Its original purpose was as part of the upgrade process that allowed SQL Server 6.5 databases to be migrated to SQL Server 7.0 and 2000. However it is also very useful in a day-to-day environment, as it allows you to programmatically generate scheduled scripts of database objects.
Not sure if this will work for SQL 2005/2008. Check this document for full details of this utility.
Raj
I had to do something similar in a past project. I wrote a simple C# console application using Sql Server Management Objects. You can write code as simple as this (may need some tweaking):
Server myServer = new Server("name");
Database db = myServer.Databases["name"];
foreach (StoredProcedure sp in db.StoredProcedures)
{
File.WriteAllText(sp.Name, sp.TextBody);
}
I wrote a utility for this task, SMOscript.
It's based on the principle of SCPTXFR (which Raj mentioned), and also supports the new object types in SQL 2005 and 2008. Script generation is performed by the SMO library.
Powershell script + object Microsoft.SqlServer.Management.Smo.Scripter? See http://blogs.msdn.com/buckwoody/archive/2009/07/02/powershell-and-sql-server-script-all-tables.aspx for an example showing how to get started w/tables. You'd have to extend the idea to other objects.