I am trying to code a website similar to reddit where people can post stories and then vote on them
So right now, I have some Python code to create a time score (PostDate) and a general rating score (VoteStatus), however I need to know how to reference/manipulate fields in an SQL table with Python.
Any help?
import datetime
def PostDate(PostID):
dday = datetime.fromtimestamp(#I need a way to reference the timestamp
#field of a specifc PostID entry in each of these blank parens).day
mmonth = datetime.fromtimestamp().month
yyear = datetime.fromtimestamp().year
hhour = datetime.fromtimestamp().hour
mminute = datetime.fromtimestamp().minute
ssecond = datetime.fromtimestamp().second
return ((dday * 1000000) + (mmonth * 100000000) + (yyear * 10000000000) + (hhour * 10000) + (mminute * 100) + ssecond)
def VoteStatus(PostID):
TimeScore = PostDate(Post)
cday = datetime.now.day
cmonth = datetime.now.month
cyear = datetime.now.year
chour = datetime.now.hour
cminute = datetime.now.minute
csecond = datetime.now.second
Now = ((cday * 1000000) + (cmonth * 100000000) + (cyear * 10000000000) + (chour * 10000) + (cminu开发者_运维百科te * 100) + csecond)
timestatus = (Now - TimeScore)
votes = #Reference the value given in the total votes field in the SQL table
return (.45 * timestatus) + (.55 * votes)
SELECT * FROM Database ORDER BY VoteStatus
For MySQL, see this question: Python MySQL module
There is also an sqlite3
module that you can use on a flat sqlite file, like so:
import sqlite3
conn = sqlite3.connect('mydb.db')
rows = list(conn.execute("SELECT * FROM Database ORDER BY VoteStatus"))
精彩评论