Thursday, November 5, 2015

Unique id generation and db connection using python

Unlike Others here I'm presenting simple functions that can be used in real time that can make your Task simple and fast.. just like plug and play. All you need to have is python installed and relevant package need to be installed in your machine..

1. Simple code to Generate unique no based on time stamp:
import time
def getUid():
    current_milli_time = lambda: int(round(time.time() * 1000))
    uid=current_milli_time()
    return uid
Here is the output for the above code:

>>> getUid()
1446793779569L
>>> getUid()
1446793788728L

'L' will not be printer when we use any IDE like Eclipse or some other....

2. Code to connect to OracleDB and Fetch the data:

For this Code we need to install 'cx_Oracle' module..
import cx_Oracle
db = cx_Oracle.connect('USER/PASSWORD@IP.address.machine:port_number/SID')
cursor = db.cursor()
cursor.execute("select count(*) from table_name")
row = cursor.fetchone()
value=row[0]
print (value)
cursor.close()
db.close()
Here is the output for the above code:
ex: if the connection string is  'USER/PASSWORD@127.0.0.1:1521/orcl'
158

No comments:

Post a Comment