2

I want to restore DB on MS SQL Server 2008 using python3.2/tkinter. Do I have any commands that would rename(if it exists already) DB and then restore it. How can I do it from python only?

Thanks

1 Answer 1

1

You can simply use _mssql from pymssql.

import _mssql

conn = _mssql.connect(server='', user='', password='', database='')
conn.execute_non_query("IF EXISTS (SELECT 0 FROM sys.databases WHERE name = 'mydb') BEGIN ALTER DATABASE mydb MODIFY NAME = mydb_old END")
conn.execute_non_query("RESTORE DATABASE mydb FROM DISK='C:\mydb.bak'")

Quick, MSSQL-y and easy.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually, I just have pythone3.2 only. Its is on office server, They won't let me install extra modules etc. I guess there are windows commands for SQL Server that would let you back-up and restore DB from command prompt?
@AashiqHussain There is sqlcmd utility, which you can pass commands to I guess: cyberciti.biz/faq/howto-ms-sql-list-tables-and-database
I want to execute commands without entering user name and password. I would like to execute it as current logged on user. How to that stuff from a python script. Thanks for the response.
@AashiqHussain Just sqlcmd -S <server>. First result on google.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.