私はPythonの初心者で、SQLiteを使用しています。だから私と一緒に忍耐してください。私はどのくらいの情報を提供すべきかを完全には分かっていないので、私は関連性があると思うほど多くのコードを記述することに決めました。諺のように。転ばぬ先の杖。SQLiteデータベースのデータをJSONでエンコードする前に辞書に読み込む方法は?
基本的に、私が持っているのは、ピアツーピアソーシャルネットワーキングウェブアプリケーションの一種のためのcherrypyサーバを実行しているpythonスクリプトです。私は私のプロフィールに3種類のアップデートを記録する方法を持っています。新しい投稿、新しい写真、または新しいイベント。
各アップデートには、以下のフィールドが含まれています。
messageID: A 16 letter string containing a unique identifier
creator: My user name
created: A time stamp, UNIX Epoch time, of when the update took place
body: A short message about the update.
Link: A link to the update. e.g.. "/gallery/photo5"
Type: type 1 means new post, 2 means photo, 3 means event.
私はSQLiteので作成されたデータベース内のテーブルの列にこれらのフィールドを作ってきた、ここで私はそれを行うために使用される方法は次のとおりです。
@cherrypy.expose
def writeUpdate(self, type=None):
"""This method is called whenever a change takes place on the Acebook
It takes an input 'type' to know what kind of update it is.
The method then make appropriet change to the 'Updates' database
"""
con = lite.connect('static/database/Updates.db')
messageID = self.randomword()
creator = trueUser
created = time.time()
if type == 1:
link = "/homepage"
body = "New Status Update"
elif type == 2:
link = "/portfolio"
body = "New Photo Update"
elif type ==3:
link = "/event"
body = "New Event Update"
else:
link = "/homepage"
body = "If you are seeing this, something is not quite right with by server"
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Updates(messageID TEXT, creator TEXT, created INT, link TEXT, type INT, body TEXT)")
cur.execute("INSERT INTO Updates VALUES(?, ?, ?, ?, ?, ?)", (messageID, creator, created, link, type, body))
"Debugging check"
cur.execute('select * from Updates')
print "The Updates database now contains:"
for row in cur:
print row
return
私の最新のニュースフィードを入手するために友人が呼び出せる別の方法があります。この方法は次のとおりです。
@cherrypy
def getActivity(self, minutes=48*60):
“”” Return any updates since last time this user requested them. Optional argument returns the last updates in the given time period instead.
“””
# current_user = getAuthenticatedUser(): # if not current_user:
# return “Please Authenticate”
# updates = getUpdatesByUser(current_user)
ExampleUpdate = [ {
‘messageID’: “ccog001-1332889924-839”, ‘creator’: “ccog001”,
‘created’: 1332889924,
‘link’: “/updates?id=839”,
‘type’: 1,
‘body’: “Hello, is anybody out there?”
},{
‘messageID’: “ccog001-1332890482-840”, ‘creator’: “ccog001”,
‘created’: 1332890482,
‘link’: “/updates?id=840”, ‘type’: 1,
‘body’: “Seriously, is this thing on?” }
]
reply = json.dumps(updates)
return reply
私の質問は、すべてでそれを符号化する前に、私は別の辞書にデータベースの個々の行を読み、[変数の例アップデートの形式に合わせてすべての辞書を組み合わせないか、ありますjson.dumps?
または、データベースにその辞書を書き込む前に、最初に辞書にmessageID、creator、created ... etc ...を書き込む方が簡単でしょうか?だから私は辞書の列を1つだけ含むデータベースに終わる?もしそうなら、どのようなコードですか?
私は非常に新しいですので、ご理解のために、コードやコメントを記入してください。
は
このタスクのどの部分に問題がありますか?データベースからの読み込み方法や 'json'エンコーダーの使い方を知っているようです。 – Marcin
@Marcin問題のビットは、私は今データベースの個々の行から別の辞書にデータを読み取ると思う。そして、 "json.dumps"でエンコーディングする前に、すべての辞書を変数 "ExampleUpdate"の形式にする。 – Synia
OK、あなたの質問を更新して、それを明確にしてください。 – Marcin