Python for sqlserver and pythonsqlserver
# Coding = gbk
Import sys
Import pymssql
# Try database connection
Try:
Conn = pymssql. connect (host = "192.168.1.43", user = "sa", password = "sa01! ", Database =" master ")
Failed t pymssql. OperationalError, msg:
Print "error: cocould not Connection SQL Server! Please check your dblink configure! "
Sys. exit ()
Else:
Cur = conn. cursor ()
# Querying databases
Query = "select name, database_id, recovery_model_desc from sys. databases"
Cur.exe cute (query)
Conn. commit
Rows = cur. fetchall ()
Print ('')
Print ('----- returned result -------')
Print ('')
For (name, database_id, recovery_model_desc) in rows:
Database_id = bytes (database_id) # convert int to string type
Print ('database name: '+ str (name) + ';')
Print ('database id: '+ database_id)
Print ('database recovery mode: '+ str (recovery_model_desc ))
Print ('')
Print ('----- all the above results! -------')
# Close the connection and release resources
Cur. close ()
Conn. close ()
How does python connect to SQL server?
The following describes how to create a MySQL stored procedure and describes the detailed steps. If you have encountered any problems with the MySQL stored procedure, you may wish to take a look at it.
-- Select database
Mysql> use test;
Database changed
-- Create an example table
Mysql> create table zzm (
-> Id int primary key auto_increment,
-> Name varchar (10)
-> );
Query OK, 0 rows affected (0.20 sec)
Mysql> insert into zzm (name) values ('zhang ');
Query OK, 1 row affected (0.08 sec)
Mysql> insert into zzm (name) values ('zeng ');
Query OK, 1 row affected (0.05 sec)
Mysql> insert into zzm (name) values ('ming ');
Query OK, 1 row affected (0.05 sec)
Mysql> select * from zzm;
+ ---- + ------- +
| Id | name |
+ ---- + ------- +
| 1 | zhang |
| 2 | zeng |
| 3 | ming |
+ ---- + ------- +
3 rows in set (0.00 sec)
-- Change the command terminator (because the default command Terminator -- semicolon (;) is often used in procedure (;)
-- Therefore, when creating a procedure, you need to define a new terminator to indicate that the command for creating a procedure ends)
-- Change the end symbol to the dollar sign -- $
Mysql> delimiter $
-- Create MySQL Stored Procedure p3
-- The stored procedure is named p3, which contains two parameters,
-- One is of the input type (marked as IN). The parameter name is nameid and the type is int,
-- One is output type (marked as OUT), the parameter name is person_name, And the type is varchar (10)
-- This stored procedure is used to query all the contents of the zzm table and output the result set.
-- Query the field name whose ID is recorded in the table and output it to the second output type parameter.
-- No result set is output.
Mysql> create procedure p3 (IN nameid int, OUT person_name varchar (10 ))
-> Begin
-> Select * from test. zzm;
-> Select zzm. name into person_name from test. zzm where zzm. id = nameid;
-> End
-> $
Query OK, 0 rows affected (0.00 sec)... the remaining full text>
How can I determine whether the operation is successful when I connect to SQL server using pymssql in python?
In general, this kind of thing will return a row in cursor. fetchone. I don't know if pymssql is like this. I have never used it.