मैं अजगर में एक एसक्यूएल और SQLite का उपयोग कर रहा हूं। तो कृपया मेरे साथ धैर्य रखें। मुझे पूरी तरह से यकीन नहीं है कि मुझे कितनी जानकारी प्रदान करनी चाहिए, इसलिए मैंने जितना लगता है उतना कोड डालने का फैसला किया है। कहने की तरह; माफी से अधिक सुरक्षित।जेएसओएन में एन्कोडिंग से पहले एक शब्दकोश में 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
:
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
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?
या अगर मैं डेटाबेस में उस शब्दकोष को लिखने से पहले, संदेश आईडी, निर्माता, बनाया ... आदि ... को पहले शब्दकोश में लिखना आसान हो सकता हूं? तो मैं डेटाबेस के साथ समाप्त होता हूं जिसमें शब्दकोशों का केवल एक स्तंभ होता है? यदि हां, तो कोड कैसा होगा?
मैं काफी नया हूं, इसलिए कृपया मुझे अपने उत्तर में विस्तृत जानकारी दें, अधिमानतः कोड और टिप्पणियों के साथ मुझे समझने में मदद करें।
आप आप समय
इस कार्य का कौन सा हिस्सा आपके लिए समस्याग्रस्त है? ऐसा लगता है कि आप डेटाबेस से कैसे पढ़ सकते हैं, और 'json' एन्कोडर का उपयोग कैसे करें। – Marcin
@ मार्सिन समस्याग्रस्त बिट मुझे लगता है कि डेटाबेस की व्यक्तिगत पंक्ति से डेटा को एक अलग शब्दकोश में पढ़ने के लिए है। और फिर "json.dumps" के साथ एन्कोडिंग से पहले, सभी शब्दकोशों को परिवर्तनीय "उदाहरण अद्यतन" के प्रारूप में डाल दें। – Synia
ठीक है, आगे बढ़ें और इसे स्पष्ट करने के लिए अपने प्रश्न को अपडेट करें। – Marcin