Escritura en una base de datos SQL server de Microsoft desde python corriendo en
linux
De los drivers que eran posible utilizar para conectarse a la BD
[https://wiki.python.org/moin/SQL%20Server ] dos opciones permitan usarse en Windows y
LINUX: pyodbc y pypyodbc.
Se decidi instalar pypyodbc por [tener un nombre gracioso] que est escrito solo en
Python.
# pip install pypyodbc
Conexin de Linux con MSSQL
La librera pypyodbc utiliza un driver para realizar la conexin. Es recomendable probar que
la conexin funcione desde la terminal de comandos y luego realizar las pruebas desde
python.
#Referencias
http://askubuntu.com/questions/578934/mssql-connection-from-ubuntu
https://code.google.com/p/pypyodbc/wiki/Linux_ODBC_in_3_steps #Conexin usando
linux
#Procedimiento ubuntu 12.04
sudo apt-get install tdsodbc unixodbc
apt-get install freetds-bin
#Test de conexion con la base de datos mediante lnea de comandos
# tsql -H xxxxxx -p xxxx -U xxxx_xxx
Password:
1> select top 1 rsam_rms from Volcanica.dbo.RSAM where esta_id_estacion='97' order by rsam_fecha_utc
desc;
2> GO
/etc/odbcinst.ini
[FreeTDS]
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
/etc/freetds/freetds.conf
# [Global]
#TDS_Version = 8.0
#client charset = UTF-8
host = xxxxxxx
port = xxxx
tds version = 8.0
#Procedimiento Centos 7 .
https://www.petri.com/how-to-connect-centos-web-server-to-a-sql-server
yum install freetds unixodbc
vi tds.driver.template
[FreeTDS]
Description = v0.91 with protocol v8.0
Driver = /usr/lib64/libtdsodbc.so.0
vi tds.datasource.template
##Configurar archivos
[DSNName]
Driver = FreeTDS
Description = TDS_Servername
Trace = No
Server = xxxxxx
Port = xxxxx
TDS Version = 8.0
Database = xxxx
# odbcinst -i -d -f tds.driver.template
odbcinst: Driver installed. Usage count increased to 1.
Target directory is /etc
# odbcinst -i -s -f tds.datasource.template
##Con los comandos anteriores se gener el archivo en /root/.odbc.ini (los comandos se
generaron con el usuario ROOT)
cp /root/.odbc.ini /etc/odbc.ini
##Test de conexin de la base de datos
[root@nodo2 seiscomp]# tsql -H xxxx -p xxxx -U xxxxx
Password:
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "ISO-8859-1"
1>
2>
3> use Volcanica;
4> Select * From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME = 'TABLENAME';
5> go
Conexin con python
import pypyodbc
conexion=pypyodbc.connect("Driver=FreeTDS;SERVER=xxxxx;port=xxx;UID=xxxxx;PWD=xxx
xxx;DATABASE=xxxxxx")
la base creada por xxxxx es
>>> cursor.execute("SELECT COLUMN_NAME FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='RSAM'")
<pypyodbc.Cursor instance at 0x1c28998>
>>>
>>> for fila in cursor:
... print fila
...
('rsam_id',)
('rsam_estacion',)
('rsam_canal',)
('rsam_fecha_ingreso',)
('rsam_fecha_proceso',)
('rsam_banda1',)
('rsam_banda2',)
('rsam_banda3',)
('rsam_banda4',)
INSERTAR DATOS
cursor.execute("insert into RSAM(rsam_estacion,rsam_canal) values('BREF','BHZ')")
CONSULTAR DATOS
>>> cursor.execute("select * from RSAM")
<pypyodbc.Cursor instance at 0x1c28998>
>>>
>>> for fila in cursor:
... print fila
...
(1, 'BREF', 'BHZ', None, None, None, None, None, None)
(2, 'BREF', 'BHZ', None, None, None, None, None, None)
(3, 'BREF', 'BHZ', None, None, None, None, None, None)