Monday, March 25, 2013

Using python pymssql to query SQLServer from linux

Getting pyssmql to work was pretty easy. (I had less luck using pyodbc.)

sudo apt-get install freetds

sudo pip install pymssql

Create entries for your sqlservers in /etc/freetds/freetds.conf like so:


[dev01]
host = YOUR_SQL_SERVER_HOST
port = 1433
tds version = 8.0



Here's a simple script using it:


import pymssql
import getpass

pwd = getpass.getpass()

conn = pymssql.connect(host='YOUR_SQL_SERVER_HOST',
                       user='YOUR_DOMAIN\\YOUR_USERNAME', password=pwd)
cur = conn.cursor()
cur.execute('use YOUR_DB')

query = "select top 10 id, name, lastmodifieddate from campaign"

cur.execute(query)

for row in cur:
    print "{0}\n{1}\n{2}\n".format(row[0], row[1], row[2])



Friday, March 8, 2013

sqsh -- install, configure, usage

install:

sudo apt-get install freetds
sudo apt-get install sqsh

Create entries for your sqlservers in /etc/freetds/freetds.conf like so:


[dev01]
host = dev01server
port = 1433
tds version = 8.0



Create settings file in ~/.sqshrc
\set username=MYDOMAIN\myusername
\set password=
\set style=vert



Usage:
user@box:~$ sqsh -Sdev01

sqsh-2.1 Copyright (C) 1995-2001 Scott C. Gray
This is free software with ABSOLUTELY NO WARRANTY
For more information type '\warranty'
1> use testdb
2> go
1> select field1, field2 from table where field1 = 'test';
2> go
field1: test
field2: other data
(1 row affected)
1> exit
user@box:~$











Tuesday, February 26, 2013

access remote django dev server from local browser


To access a remote Django development server from your local browser....

On the local host, SSH to the remote host:
$ ssh -v -L 9000:localhost:8000 <the remote address>
On the remote host, run the Django dev server:
/path/to/my/django/project$ python manage.py runserver 0.0.0.0:8000
On the local host, go to http://localhost:9000 in the browser

Or...

Using LocalForward in your ~/.ssh/config, add entry

Host myremote
  User eliot
  HostName my.remotehost.com
  LocalForward 9000 localhost:8000

Thursday, February 14, 2013

SQuirrel SQL client for Ubuntu

Provides a nice gui for accessing Microsoft SQLServer databases. (Handles MySQL also.)

Get it from here: http://www.squirrelsql.org/

Configuring it is a little tricky...use the guide here:
http://informatics.malariagen.net/2011/08/15/squirrel-sql-a-nice-database-gui-frontend-for-ubuntu/


To configure it for MySQL as well, follow similar steps after installing the mysql-jdbc driver.
sudo apt-get install libmysql-java