I wrote some python to query an Oracle database and I would like it to print the results in a formatted HTML table when I look at it with my browser. I am unsure how to do this.
The python I wrote is as below:
#!/usr/bin/python2.6
import imp,datetime
import cx_Oracle
def index():
conn_str = u'$USERNAME/$PASSWORD#$HOSTNAME:$PORT/$SERVICENAME'
conn = cx_Oracle.connect(conn_str)
c = conn.cursor()
query = c.execute(u'SELECT $FIELD1, $FIELD2, $FIELD3 FROM $TABLE')
cur = c.fetchall()
for row in cur:
print(str(row))
conn.close()
A co-worker of mine has a similar script he wrote that is immensely more complicated in terms of credentialing and cursor creation, and he uses the "write" in Python to output. With his I can at least get output to a webpage, and I can't understand why mine won't even show anything, let alone my query results. The problem is that his output comes out unformatted and even if I used his code I don't know how to give it table structure.
For contrast, his:
#!/usr/bin/python2.6
import os
os.environ["ORACLE_BASE"]="/oracle"
os.environ["ORACLE_HOME"]="/oracle/product/11.2.0/client_1"
os.environ["LD_LIBRARY_PATH"]="/oracle/product/11.2.0/client_1/lib:/oracle/product/11.2.0/client_1/dbjava/lib"
os.environ["TNS_ADMIN"]="/oracle/product/11.2.0/client_1/network/admin"
import imp,datetime
import cx_Oracle
DBCONNECTED=""
CONNECT={}
def connect(tnsname):
global DBCONNECTED
DB={}
DB['$DATABASE']=['$USER','$PASSWORD']
#print str(DB[tnsname][0]+"/"+DB[tnsname][1]+"#"+tnsname)
conn=cx_Oracle.connect(DB[tnsname][0]+"/"+DB[tnsname][1]+"#"+tnsname)
DBCONNECTED+=tnsname+":"
return conn
def getcredentials(env,user):
env=env.lower()
CRED={};CRED['$DATABASENAME']={};
CRED['$DATABASENAME']['$USERNAME']='$PASSWORD'
if env in CRED and user in CRED[env]:
return CRED[env][user]
else:
return 'ERR'
def returnconnection(dbtns):
global CONNECT
if DBCONNECTED.find(dbtns+":")==-1: #connection hasn't been initialized, do that
CONNECT[dbtns]=connect(dbtns.lower())
cur= CONNECT[dbtns].cursor()
return cur
def runq(dbtns,query,bindvar=''):
query=query.replace("\n"," ")
cur=returnconnection(dbtns)
if bindvar=='':
cur.execute(query)
else:
cur.execute(query,bindvar)
rs=cur.fetchall() #this should be fine for up to several thousand rows
return rs
def index (req,rssid=""):
global R; R=req; R.content_type="text/html"
R.write("""
<!DOCTYPE HTML">
<html><head><title>TABLES</title><META HTTP-EQUIV='Pragma' CONTENT='no-cache'>
</head>
<table>
""")
dat=runq('$DATABASE','SELECT $FIELD1, $FIELD2, $FIELD3 FROM $TABLE')
for row in dat:
R.write(str(row))
#write footer
R.write("""
</table>
</body></html>
""")
I like the simplicity of what I wrote, but my colleague is obviously doing something right to spit the output to a page. When I try and re-create his usage of "Global R" to invoke "R.write" I get a unicode error regarding the content_type module, which seems odd.
Regardless, I feel this should be insanely simple. I'm more used to PHP, and this is my first attempt at using Python to create this sort of webpage.
Ideas?
Why do you use unicode strings? Your colleague doesn't. Try without the "u" prefix on your strings.
Related
so I am working on a project in which I am displaying HTML cards based on the number of 'Events' as in, physical events (irrelevant, just for a bit of context).
Here is my code:
from flask import render_template
from app import app
connection = sqlite3.connect("./app/database.db", check_same_thread=False)
connection.row_factory = sqlite3.Row
#app.route('/')
def index():
cursor = connection.cursor()
events = cursor.execute("SELECT name, date, time, venue FROM events").fetchall()
print(events)
return render_template("index.html", events=events)
However the content of events then becomes a list of <sqlite3.Row object at 0x1071544f0>s
You will notice that I have done the .fetchall() correctly.
NOTE: The intended behaviour is achieved if I remove connection.row_factory = sqlite3.Row but then the format is not what I want. I need the output to be a list[] of dictionaries{} and without that line, the output is a list[] of strings containing parenthesis (If I've understood?)
Can someone shed some light?
I am used to CS50 (Harvard's course)'s SQL library which is very similar but simplified for learning, in which all you have to do is db=database and then db.execute("")
Thank you guys!
I am trying to retrieve data from a mysql server, run a code using the data retrieved, then push the data back to the database on different columns. I have managed to get all of that working as seen in my code attached.
However, new rows will be added constantly to the database and i don't want to have to load the entire database everytime. I want the code to only run on the new rows that have been added. Therefore, i am trying save the student_id and retrieve it somehow and the beginning of the code.
I tried different methods and i even tried pickle but i was left with little luck. The aim is to have the code run continuously on a remote server so i would really appreciate if someone can help me out as it will help me achieve my task.
Note: This is only dummy data and code but is pretty much the method of what i am trying to achieve.
import mysql.connector
import numpy as np
import time
import pickle
filename = 'saved_id'
infile = open(filename,'rb')
st_id = pickle.load(infile)
print(number)
conn = mysql.connector.connect(user="user", password='password',
host="demo.mysql.database.azure.com", port=3306)
conn.autocommit = True
cursor = conn.cursor()
cursor.execute("USE testdb;")
while True:
outfile = open(filename,'wb')
cursor.execute(f"SELECT * FROM student WHERE student_id >= {st_id};")
myresult = np.array(cursor.fetchall())
print (myresult)
for row in myresult:
mark_1 = np.random.randint(0,100)
mark_2 = np.random.randint(0,100)
mark_3 = np.random.randint(0,100)
iid = row[0]
pickle.dump(iid,outfile)
outfile.close()
cursor.execute(f"UPDATE student SET grade_1 = {mark_1}, grade_2 = {mark_2}, grade_3 = {mark_3} WHERE student_id = {iid};")
time.sleep(5)
I Think the problem is that when i initially run the code the pickle value doesn't have an input, and when i try to use a standard variable method it will not be defined which will result in some kind of error as well.
Please also do let me know if there are any aspects of my code that could be improved for speed, efficiency!
I'm writing my python program using IDLE and whenever I run
python3 [myfilename]
in my terminal it doesn't output what it's supposed to be outputting from the SQL query I made.
I've tried to see if the problem was with these 2 lines:
c = db.cursor()
c.execute("select content time from posts order by time desc")
and guessed that maybe
db.cursor() = c
db.cursor(execute("select * from posts))
posts = db.fetchall()
print(add_post());
would actually add something but I get no output! Basically, I want it to print out anything from my database.
def get_posts():
db = psycopg2.connect(database="forum")
c = db.cursor()
c.execute("select content, time from posts order by time desc")
posts = c.fetchall()
db.close()
get_posts()
print(get_posts)
I expect my output to print any data in terminal but running the file doesn't actually print anything. Please help!
EDIT: my new error in IDLE is "Is the server running locally and accepting
connections on Unix domain socket"
can anybody help me out here with this?
You need to return something from your get_posts function. As it is your printing the function object get_posts, which will look something like <function get_post at 0x7f42d60bcf28>
Your could ought to look something like this:
def get_posts():
db = psycopg2.connect(database="forum")
c = db.cursor()
c.execute("""select content, time from posts order by time desc""")
posts = c.fetchall()
db.close()
# Return a value
return posts
# Call the function and assign the return value to a variable
the_posts = get_posts()
# Print the variable
print(the_posts)
I am writing a script in python 3.x using mysqlconnector.
What I am trying to achieve right now is to check if there is a record inside my db which may be a duplicate to the one I am analyzing right now.
I came up with such code:
def fill_data(self, db_name, data):
cursor = self.cnx.cursor(buffered=True)
isDuplicate = cursor.execute(("SELECT destination FROM {0} WHERE destination = '{1}';")
.format(db_name, data['destination']))
print(cursor.statement)
self.commit()
print(isDuplicate is None)
Though I still get isDuplicate as None object. I tried to check via cursor.statement what statement is being passed to my db: it turned out that while in script I get None obj while passed in db that query works fine.
I also tried SELECT COUNT(1) FROM db_name which also gave me different results.
I am out of ideas: maybe you guys can help me out?
Update:
The solution that works for me was:
q = ("SELECT * FROM {0} WHERE destination = %s AND countryCode = %s AND prefix = %s")
.format(db_name)
cursor.execute(q, (data['destination'], data['country_code'], data['prefix']))
self.cnx.commit()
isDoubled = cursor.fetchone()
So at the end of the day it was all about fetching data from the cursor :)
Maybe the reason of your issue is the way you use execute() method.
Try to make some changes and see what is printed out:
def fill_data(self, db_name, data):
cursor = self.cnx.cursor(buffered=True)
q = 'SELECT count(*) FROM {} WHERE destination = %s'.format(db_name)
duplicate_count = cursor.execute(q, (data['destination'], )).fetchall()
print(duplicate_count)
Why should I provide query parameters this way? (article is on psql, but the core principles are the same as in mysql)
update
If you are still receiving "NoneType" object has no atribute "fetchall", then the error is probably here:
cursor = self.cnx.cursor(buffered=True)
Looks like you are not creating cursor at all. I can take a look at it if you post some code about cnx creation.
I am using cx_Oracle to fetch some data stored in Arabic characters from an Oracle database. Below is how I try to connect to the database. When I try to print the results, specially those columns stored in Arabic, I get something like "?????" which seems to me that the data was not coded properly.
I tried to print random Arabic string in Python it went alright, which indicates the problem is in the manner in which I am pulling data from the database.
connection = cx_Oracle.connect(username, password, instanceName)
wells = getWells(connection)
def getWells(conn):
cursor = conn.cursor()
wells = []
cursor.execute(sql)
clmns = len(cursor.description)
for row in cursor.fetchall():
print row
well = {}
for i in range(0, clmns):
if type(row[i]) is not datetime.datetime:
well[cursor.description[i][0]] = row[i]
else:
well[cursor.description[i][0]] = row[i].isoformat()
wells.append(well)
cursor.close()
connection.close()
return wells
In order to force a reset of the default encoding from the environment, you can call the setdefaultencoding method in the sys module.
As this is not recommended, it is not visible by default and a reload is required.
It is recommended that you attempt to fix the encoding set in the shell for the user on the host system rather than modifying in a script.
import sys
reload(sys)
sys.setdefaultencoding('utf-8')