Sunday, September 30, 2012

Connect to MS SQL Server using Python

Steps to take to connect to a MS SQL Server using Python. In this case using the python ODBC module pyodbc.
1. Download pyodbc from http://code.google.com/p/pyodbc/downloads/list, get the newest 32-bit version (even if you have 64-bit OS), since you need to have a paid license to use the 64-bit version. The 32-bit version is free.
2. Check what ODBC client you have for MS SQL Server. Go to Control Panel -> Administrative Tools -> Data Sources (ODBC) -> Drivers tab (on Windows 7). You will hopefully see a list of drivers to choose from. I had SQL Server and SQL Server Native Client 10.0. You will need the whole name in the next step.
3. Here is a sample program to fetch data from a table:
# -*- coding: utf-8 -*-
import pyodbc

CONNECTION_STRING="""
Driver={SQL Server Native Client 10.0};
Server=localhost\sqlexpress;
Database=MYDB_DB;
Trusted_Connection=yes;
"""

db = pyodbc.connect(CONNECTION_STRING)
c = db.cursor()
c.execute ('SELECT * FROM users')
rs = c.fetchall()
for r in rs:
    print r[0]

Notice how we put the driver name from step 2. into the Driver parameter, the Server parameter should point to your server and the Database should be the name of your database. The triple quotations is just to let us have the connection string on multiple lines in the code, you could use a single quotation mark and have it all on the same line.

Sources: http://community.activestate.com/forum/how-instal-pyodbc-module, http://stackoverflow.com/questions/289978/whats-the-simplest-way-to-access-mssql-with-python-or-ironpython, http://code.google.com/p/pyodbc/wiki/GettingStarted, http://stackoverflow.com/questions/10558354/using-microsoft-access-database-mdb-with-python-on-ubuntu

Tuesday, September 18, 2012

Python writing UTF-8 files

Some steps to take to create UTF-8 files with Python:
1. use codecs module to read and write files:
import codecs
f = codecs.open('file.txt', mode="w", encoding="utf-8-sig")
2. Don't mix strings and unicode. (Always prefix "strings" with u (e.g. u"hello world"))
3. Start your python script with # -*- coding: utf-8 -*- in case you might have any unicode characters in your code or in hardcoded "strings" in your script.

4. Also read http://lobstertech.com/python_unicode.html and http://www.evanjones.ca/python-utf8.html for more info on using UTF-8/unicode with Python.

Other sources:
http://docs.python.org/howto/unicode.html
http://www.carlosble.com/2010/12/understanding-python-and-unicode/

Transact SQL Arithmetic overflow error

Getting
Arithmetic overflow error converting numeric to data type numeric.
when trying to insert into your MS SQL (Transact SQL) table?

Most probably this is caused by the datatype of your field. If the type is for example numeric(18,18) this does not mean that you can have 18 digits before the decimal point and 18 after. It actually means that you can have a total of 18 digits (specified by the first value) and 18 digits after the decimal point.

As you might realize this means you can't have any values larger or equal than 1.0 in your field.

So, if that's what your're trying to do, change the field type to for example numeric(18,5) which will give you 5 digits in front of the decimal point and 13 after.

Source: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=35602

Thursday, August 23, 2012

.NET XAML binding more than one property

When making views using XAML and with .NET you might sometimes want to combine more than one property into a textblock (e.g. for example showing the breadcrumbs of your location in a textblock). Something like the following for example:
Location1.Title + "." + Location2.Title + "." + Location3.Title

There are several ways of doing this.

One is creating a custom converter for MultiBinding that creates the string for you, pretty well explained here:
http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.converter.aspx
Which seems to be the common suggestion, but it involves writing a lot of code for just one field. And you might have to do this often!

Alternatively you can just combine a bunch of textblocks:
<textblock text="{Binding Path=Location1.Title}" />
<textblock text=", " />
<textblock text="{Binding Path=Location2.Title}" />
<textblock text=", " />
<textblock text="{Binding Path=Location3.Title}" />
Which is ok, but gets messy.

Or, you can (Since .NET 3.5 SP1) use MultiBinding property StringFormat:
<TextBlock Margin="5,0,0,0">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}, {2}">
<Binding Path="Location1.Title"/>
<Binding Path="Location2.Title"/>
<Binding Path="Location3.Title"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>

Note the empty bracket at the start of the StringFormat. When the XAML reader sees an opening bracket it reads this as the start of a markup extension (Binding). The empty bracket is an escape sequence to prevent the XAML reader from trying to interpret the {0} as a binding.
Note2: A space character will also work (" {0}, {1}, {2}").

Sources: http://stackoverflow.com/questions/505397/built-in-wpf-ivalueconverters, http://stackoverflow.com/questions/9001974/strange-multibinding-stringformat-issue, http://msdn.microsoft.com/en-us/library/ms744986.aspx

Friday, August 3, 2012

MySQL VS Oracle SQL

Listed below are ways to achieve the same in Oracle SQL as in MySQL:

The equivalent of LIMIT in MySQL is ROWNUM in Oracle SQL:
MySQL: SELECT * FROM table LIMIT 10;
Oracle SQL: SELECT * FROM table WHERE ROWNUM <= 10

Solution found at: http://www.delphifaq.com/faq/databases/oracle/f594.shtml

Thursday, July 26, 2012

Stop Firefox download window from blinking

The window that opens when you download a file in Firefox may sometimes be annoying, either because it pops up when you're trying to do something else or that the button/window on the windows taskbar starts to blink.

Firefox lets you disable these features through its advanced properties. Enter about:config into the address bar of Firefox, accept the "I'll be careful, I promise!" warning. Then enter one of the following in the Search field at the top:

browser.download.manager.flashCount - set to 0 (zero) to disable blinking in windows taskbar.

browser.download.manager.showWhenStarting - set to false to prevent the download window from showing at all (it won't open if it's not already open) when you start a download.

browser.download.manager.focusWhenStarting - set to false to keep the download window from stealing focus/showing when a download starts.

Almost directly copied from http://superuser.com/questions/63762/is-there-way-to-disable-download-bar-blinking-in-firefox, thanks to Adam Ryan. :)

Tuesday, May 29, 2012

Windows Phone 7 podcast workaround

In any other country than the U.S., podcasts are not available in the WP7 marketplace. To get podcasts you can either use a third party app (like Simply Podcast which streams your podcasts when you requests them) or use the Zune software on your computer.

This might sound horrible, "I have to connect my phone to my computer to get podcast like some ipod". However, once you've added the podcast to your phone in the Zune software you can then go to Music+Videos on your phone, select Subscribe and have your phone automatically download the latest episodes when it is connected to a Wifi network.

Just slightly annoying. :)