Python MS-SQL (pyodbc)


Hello,

I need to create a database, user, schema and role programmatically.

conn= pyodbc.connect('DRIVER={SQL Server};SERVER=(local);DATABASE=master;UID=sa;PWD=xxxxxx', autocommit=True)
 cursor = conn.cursor()

cursor.execute("CREATE DATABASE test")

till here works fine.

Now, I want to change the connection from master DB to the new created test DB and create login, user and everything.

I have tried to close the connection and open a new connection to the newly created test DB and created user id, did not worked.

I also have tried in another way of creating a string

sql="CREATE DATABASE test
GO
USE test
GO
CREATE LOGIN xx WITH PASSWORD='xx'
GO
CREATE USER xxx FROM LOGIN xx
GO
CREATE SCHEMA xxy AUTHORIZATION xx
GO
ALTER USER xxx WITH DEFAULT_SCHEMA = xxy
GO
 
CREATE ROLE author
GO"

cursor.execute(sql)

 and tried to execute this sql string, did not worked too.

Please let me know what mistake I am doing here. Thanks for your help in advance.
asudhaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mish33Commented:
You need a separate .execute() call for every CREATE/ALTER/.
'GO' is only a feature of an interactive prompt and can't be used with programmatic connection.

IOW, supply one one sql statement for .execute.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asudhaaAuthor Commented:
Hi,

I tried using one .execute for each create but did not worked too.

conn= pyodbc.connect('DRIVER={SQL Server};SERVER=(local);DATABASE=master;UID=sa;PWD=xxxxxx', autocommit=True)
 cursor = conn.cursor()

cursor.execute("CREATE DATABASE test")
cursor.close()
conn.close()

*** til here it worked***** Now want to change the connection to the newly created DB test to create user*****

conn1= pyodbc.connect('DRIVER={SQL Server};SERVER=(local);DATABASE=test;Trusted_Connection=Yes, autocommit=True)
 cursor1 = conn1.cursor()

cursor1.execute("CREATE LOGIN xx WITH PASSWORD='xx'")
cursor1.execute("CREATE USER yy FROM LOGIN xx")

In this way too it did not have created login and user.

Am I following the correct steps or am I missing something in between?

Thanks.
0
Bhavesh ShahLead AnalysistCommented:
Hi,

Did you tried same query in backend?

Is it working??


- Bhavesh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.