Python: Connecting to SQL Server with pymssql (Windows Authentication)

from os import getenv
import pymssql


conn = pymssql.connect(server = 'localhost', database = 'VCDB')
cursor = conn.cursor()
cursor.execute('select * from vpx_version')
row = cursor.fetchone()
while row:
    print("ID=%d, Name=%s" % (row[0], row[1]))
    row = cursor.fetchone()
conn.close()




Python: setting remote view in Raspberry Pi

In the Raspberry Pi box


Installing tightvncserver.

sudo apt-get install tightvncserver

Set it up at port #1. One can type in any other ports as long as not conflicting with other open ports.

sudo vncserver :1 -geometry 1920x1080 -depth 24


If you do not know your IP address just do "ifconfig" and look for the ip address because you are going to need this later.

ifconfig|grep inet


In another box, (I have an Ubuntu in this case) install 




sudo apt-get install xtightvncviewer

To run it simply type in the following

xtightvncviewer <IP>:port


Example: xtightvncviewer 192.168.1.123:1



Other easier option would be to download VNCViewer from Google Chrome Web Store.

Python: Extracting json and inserting into sqllite3

This is an interesting project that combining python, json and sqllite to extract, store and processing the data.

The first part are recreating the required tables. Second, part is to read and extract the roster1 json file and capturing the records and arranging them in columns (name, title and role). Nowthat we have the records for all 3 columns and inserting them into all dedicated tables.

Codes are presented in screenshots on purpose to deter from easy google searching for solution as this is a course assignment.

Here is an example of how does the role table looks like.



What I learned here are the embedded SQL in the executescript and the anchoring of the binding variables.  With this simple algorithm and know-how, I can do a lot of more complicated applications.

Python: Raspberry Pi: Adding Button to control LED blinking

Second Project

This is an extension of the first project where letting the python to control the LED blinking. Instead of that, I am going to a physical button to control the LED blinking instead.

Requirements are, when button not pressed, the LED blinks and when the button pressed, the LED lid continuously.

This project is slightly trickier as more wires involved but not terribly bad. Just be cautious of where the wires go.

I am rewiring everything from the first project as I need to cram the LED and the button in the tiny breadboard.

My new pin assignments are the following
pin # 27: LED
pin # 4 :  Button

Picture of the breadboard




Code:




https://youtu.be/UARP5Cuh4FY

Python: Raspberry Pi: First project: LED Blinking

First project


This is an easy project that using the Raspberry Pi GPIO 40 pin to control the LED blinking with Python GPIO library.

I would not be going through the process of putting the wires together on the breadboard. Picture tells it all. Just remember to ground it and have a resistor.



I am using pin 4 for the LED

Code:


import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT)
while True:
    GPIO.output(24, True)
    print "Led On"
    time.sleep(3)
    GPIO.output(4, False)
    print "Led Off"
    time.sleep(2)

Not finding oracle libraries

Just an indication that the python cx_Oracle did not find the libraries.

[oracle@localhost samples]$ python vpxadmin.py Traceback (most recent call last):
  File "vpxadmin.py", line 1, in <module>
    import cx_Oracle
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory [oracle@localhost samples]$ python vpxadmin.py Traceback (most recent call last):
  File "vpxadmin.py", line 1, in <module>
    import cx_Oracle
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory [oracle@localhost samples]$ . oraenv ORACLE_SID = [oracle] ? orcl The Oracle base for

ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_1 is /home/oracle/app/oracle


Just do the following. Assuming orcl is your instance name which is also a default.

. oraenv
orcl

Install cx_Oracle

Download the gz file and extract it.
http://cx-oracle.sourceforge.net/

Switch to Oracle user as it will need Oracle libraries to perform the installation.
su - oracle

important: switch to the oracle environment you would like to install this. By doing it, . oraenv, it will locate the right Oracle Home and knowing where all the libraries reside. My instance is "orcl"

. oraenv

orcl


The installation part.

python setup.py build

pyhton setup.py install

During the cx_Oracle copying stage, it might fail to copy (cx_Oracle.so) specific libraries path to the python directories. Just perform a chmod 755 on the python package_site path with root if the python has been installed with root previously.

That's it.

There are some examples in the "samples" directory you can try it out. The DatabaseStartup.py and DatabaseShutdown.py are pretty cool.