我正在尝试编写一个类似于reddit的网站,人们可以在其中发布故事,然后对其进行投票
所以现在,我有一些Python代码来创建时间分数(PostDate)和一般评级分数(VoteStatus),但是我需要知道如何使用Python引用/操作SQL表中的字段。
有什么帮助吗?
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) + (cminute * 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
对于 MySQL,请参阅此问题:Python MySQL 模块
还有一个sqlite3
模块,您可以在平面 sqlite 文件上使用,如下所示:
import sqlite3
conn = sqlite3.connect('mydb.db')
rows = list(conn.execute("SELECT * FROM Database ORDER BY VoteStatus"))