Added README.md and updated main.py
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
# sql_2000_connect
|
||||
A short Python program that demonstrates connecting to a SQL Server 2000 database. The demo simply retrieves results from the [JobMessageQueue]
|
||||
SQL table and prints them out to the console.
|
||||
|
||||
## Non-standard libraries used
|
||||
* pyodbc
|
||||
|
||||
|
||||
### Getting started
|
||||
Fairly simple in getting this demo working, this demo is intended to work on Windows operating systems. This was tested on Windows 10, although it's expected to work for Windows Operating Systems from Windows 7 to Windows 10. Prior to cloning the repository ensure that the following has been addressed:
|
||||
|
||||
1. Install Python (Python3 or greater)
|
||||
2. Install pip
|
||||
3. Install virtualenv
|
||||
4. Install the SQL Native Client driver
|
||||
|
||||
|
||||
May have to add python, pip, and virtualenv to the PATH enviornment in order to call the software from the command line
|
||||
|
||||
## Building and running
|
||||
Clone the repository to a directory of your choice and enter the directory.
|
||||
```Bash
|
||||
git clone https://github.com/a1admin/sql_2000_connect
|
||||
cd sql_2000_connect
|
||||
```
|
||||
|
||||
Ensure the ``connection_string.json`` file contains the appropriate values to connect to the SQL Server 2000 database.
|
||||
|
||||
``connection_string.json``
|
||||
```Json
|
||||
{
|
||||
"driver": "SQL Native Client",
|
||||
"server": "servername",
|
||||
"database": "databasename",
|
||||
"username": "admin",
|
||||
"password": "password"
|
||||
}
|
||||
```
|
||||
|
||||
Create a virtuel environment using ``virtualenv`` and activate the environment
|
||||
|
||||
```Bash
|
||||
virtualenv env
|
||||
env\Scripts\activate
|
||||
```
|
||||
|
||||
A new virtual enviornment was created in the env directory. The purpose of this is to contain packages/dependencies of the software to be within
|
||||
the project and not installed on the system. This makes it easy when working on the same project with multiple people. For the most part no need
|
||||
to worry about project dependencies that haven't been installed.
|
||||
|
||||
Install the ``pyodbc`` dependency required for this project
|
||||
```Bash
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
|
||||
Now run the program and pass the path of the ``connection_string.json`` as a command line argument.
|
||||
```Bash
|
||||
python3 main.py path\to\connection_string.json
|
||||
```
|
||||
|
||||
The result is the last 15 [JobMessageQueue] records created at the time the program fetched the results. In other words, if one were to run this
|
||||
program again, the results might change due to a message being queued to a user.
|
||||
@@ -55,27 +55,42 @@ class ConnectionString:
|
||||
password = ""
|
||||
|
||||
|
||||
# Retrieves queued messages of a given user
|
||||
def retrieve_queued_messages(conn_string, user):
|
||||
# Retrieves queued messages of a given user. If no user is given then the last
|
||||
# 15 records will be returned
|
||||
def retrieve_queued_messages(conn_string, user = ""):
|
||||
messages = collections.defaultdict(JobMessageQueue)
|
||||
|
||||
message_cutoff = 15
|
||||
|
||||
# Opens a connection to the database
|
||||
conn = pyodbc.connect(conn_string)
|
||||
|
||||
# Creates a cursor object. From this object one can make queries against
|
||||
# the database
|
||||
curs = conn.cursor()
|
||||
|
||||
i = 0
|
||||
|
||||
if user == "":
|
||||
query = "SELECT * FROM [JobMessageQueue] ORDER BY Rec_id DESC"
|
||||
objs = curs.execute(query)
|
||||
else:
|
||||
query = "SELECT * FROM [JobMessageQueue] WHERE UserID = ? ORDER BY Rec_id DESC"
|
||||
objs = curs.execute(query, user)
|
||||
|
||||
|
||||
# Iterates through each row returned from the query and adds the record
|
||||
# to our user-defined dictionary messages
|
||||
for row in curs.execute("SELECT * FROM [JobMessageQueue] WHERE UserID = ?", user):
|
||||
for row in objs:
|
||||
message = JobMessageQueue(row.Rec_id, row.UserID, row.Message, row.Occurred)
|
||||
message.cleared = row.Cleared
|
||||
message.job_key = row.JobKey
|
||||
|
||||
messages[i] = message
|
||||
i += 1
|
||||
|
||||
if i > message_cutoff:
|
||||
break
|
||||
|
||||
conn.close()
|
||||
|
||||
@@ -126,8 +141,6 @@ def print_queued_messages(messages):
|
||||
|
||||
|
||||
def main():
|
||||
print("In the main function")
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print("Include path to the connection string file")
|
||||
sys.exit(-1)
|
||||
@@ -139,7 +152,7 @@ def main():
|
||||
|
||||
print("Connection string is %s" % (conn_string))
|
||||
|
||||
messages = retrieve_queued_messages(conn_string, "kdeng")
|
||||
messages = retrieve_queued_messages(conn_string)
|
||||
|
||||
print_queued_messages(messages)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user