2010-11-22 10 views
11

का उपयोग कर JSS को MySQL में सम्मिलित करना मेरे पास पाइथन में एक JSON ऑब्जेक्ट है। मैं पाइथन डीबी-एपीआई और सिंपलजेसन का उपयोग कर रहा हूं। मैं जेएसन को एक MySQL तालिका में डालने की कोशिश कर रहा हूं।पायथन

पल में त्रुटियां हो रही हैं और मेरा मानना ​​है कि यह जेएसओएन ऑब्जेक्ट्स में एकल कोट्स के कारण है।

पाइथन का उपयोग करके मैं अपना JSON ऑब्जेक्ट MySQL में कैसे डाल सकता हूं?

error: uncaptured python exception, closing channel 
<twitstream.twitasync.TwitterStreamPOST connected at 
0x7ff68f91d7e8> (<class '_mysql_exceptions.ProgrammingError'>: 
(1064, "You have an error in your SQL syntax; check the 
manual that corresponds to your MySQL server version for 
the right syntax to use near ''favorited': '0', 
'in_reply_to_user_id': '52063869', 'contributors': 
'NULL', 'tr' at line 1") 
[/usr/lib/python2.5/asyncore.py|read|68] 
[/usr/lib/python2.5/asyncore.py|handle_read_event|390] 
[/usr/lib/python2.5/asynchat.py|handle_read|137] 
[/usr/lib/python2.5/site-packages/twitstream-0.1-py2.5.egg/ 
twitstream/twitasync.py|found_terminator|55] [twitter.py|callback|26] 
[build/bdist.linux-x86_64/egg/MySQLdb/cursors.py|execute|166] 
[build/bdist.linux-x86_64/egg/MySQLdb/connections.py|defaulterrorhandler|35]) 

संदर्भ

error: uncaptured python exception, closing channel 
<twitstream.twitasync.TwitterStreamPOST connected at 
0x7feb9d52b7e8> (<class '_mysql_exceptions.ProgrammingError'>: 
(1064, "You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the right 
syntax to use near 'RT @tweetmeme The Best BlackBerry Pearl 
Cell Phone Covers http://bit.ly/9WtwUO''' at line 1") 
[/usr/lib/python2.5/asyncore.py|read|68] 
[/usr/lib/python2.5/asyncore.py|handle_read_event|390] 
[/usr/lib/python2.5/asynchat.py|handle_read|137] 
[/usr/lib/python2.5/site-packages/twitstream-0.1- 
py2.5.egg/twitstream/twitasync.py|found_terminator|55] 
[twitter.py|callback|28] [build/bdist.linux- 
x86_64/egg/MySQLdb/cursors.py|execute|166] [build/bdist.linux- 
x86_64/egg/MySQLdb/connections.py|defaulterrorhandler|35]) 

यहाँ के लिए एक और त्रुटि है कि मैं http://pastebin.com/q5QSfYLa

#!/usr/bin/env python 

try: 
     import json as simplejson 
except ImportError: 
     import simplejson 

import twitstream 
import MySQLdb 

USER = '' 
PASS = '' 

USAGE = """%prog""" 


conn = MySQLdb.connect(host = "", 
         user = "", 
         passwd = "", 
         db = "") 

# Define a function/callable to be called on every status: 
def callback(status): 

    twitdb = conn.cursor() 
    twitdb.execute ("INSERT INTO tweets_unprocessed (text, created_at, twitter_id, user_id, user_screen_name, json) VALUES (%s,%s,%s,%s,%s,%s)",(status.get('text'), status.get('created_at'), status.get('id'), status.get('user', {}).get('id'), status.get('user', {}).get('screen_name'), status)) 

    # print status 
    #print "%s:\t%s\n" % (status.get('user', {}).get('screen_name'), status.get('text')) 

if __name__ == '__main__': 
    # Call a specific API method from the twitstream module: 
    # stream = twitstream.spritzer(USER, PASS, callback) 

    twitstream.parser.usage = USAGE 
    (options, args) = twitstream.parser.parse_args() 

    if len(args) < 1: 
     args = ['Blackberry'] 

    stream = twitstream.track(USER, PASS, callback, args, options.debug, engine=options.engine) 

    # Loop forever on the streaming call: 
    stream.run() 
+0

में एक पाठ क्षेत्र में सम्मिलित कर सकते हैं संदेश सहायक होगा :) – shawnjan

+0

वास्तव में डेटाबेस में JSON डेटा डालने के लिए आप किस कोड का उपयोग कर रहे हैं? –

+1

उम्मीद है कि एक गूंगा सवाल है, लेकिन क्या आप (उम्मीद है) बाध्य चर का उपयोग कर रहे हैं? – Gerrat

उत्तर

15

उपयोग json.dumps (json_value) एक json स्ट्रिंग में अपने json वस्तु (अजगर वस्तु) कन्वर्ट करने के लिए है कि आप त्रुटि की एक प्रति mysql

http://docs.python.org/library/json.html

0

उपयोग कर रहा हूँ कोड का कोई लिंक है:

यहाँ मैं प्राप्त त्रुटि संदेश है त्रुटि उस फ़ील्ड के आकार के ओवरफ़्लो के कारण हो सकती है जिसमें आप अपना जेसन डालने का प्रयास करते हैं। किसी भी कोड के बिना, आपकी मदद करना मुश्किल है।

क्या आपने सोचडब जैसे नो-एसक्यूएल डेटाबेस सिस्टम पर विचार किया है, जो एक दस्तावेज उन्मुख डेटाबेस जेसन प्रारूप पर निर्भर है?

+0

MySQL केवल बाहरी वेबसाइट के साथ उपयोग के लिए विकल्प है ... – Aran

+0

ओपी द्वारा दिखाए गए दोनों त्रुटियों में निम्न शामिल हैं: "आपके SQL वाक्यविन्यास में त्रुटि है ..."। इसका कोई ओवरफ़्लो नहीं है क्षेत्र का आकार – Gerrat

1

आप एक पाठ या ब्लॉब स्तंभ intyo सम्मिलित करने के लिए आसानी से

db.execute("INSERT INTO json_col VALUES %s", json_value) 
4

अन्य उत्तर पर विस्तार करने के लिए सक्षम होना चाहिए:

  1. :

    मूल रूप से आप दो चीजों के लिए सुनिश्चित करें की जरूरत है

    कि आपके पास उस क्षेत्र में डालने वाले डेटा की पूरी मात्रा के लिए कमरा है जिसे आप इसे रखने का प्रयास कर रहे हैं। विभिन्न डेटाबेस फ़ील्ड प्रकार डेटा की अलग-अलग मात्रा में फिट हो सकते हैं। देखें: MySQL String Datatypes। आप शायद "टेक्स्ट" या "बीएलओबी" प्रकार चाहते हैं।

  2. कि आप सुरक्षित रूप से डेटाबेस को डेटा पास कर रहे हैं। डेटा पास करने के कुछ तरीके डेटाबेस को डेटा पर "देखने" का कारण बन सकते हैं और यदि डेटा SQL की तरह दिखता है तो यह भ्रमित हो जाएगा। यह एक सुरक्षा जोखिम भी है। देखें: SQL Injection

समाधान # 1 के लिए कि डेटाबेस सही क्षेत्र प्रकार के साथ बनाया गया है की जाँच करने के लिए है।

# 2 का समाधान पैरामीटरयुक्त (बाध्य) प्रश्नों का उपयोग करता है। उदाहरण के लिए, के बजाय के लिए:

# Simple, but naive, method. 
# Notice that you are passing in 1 large argument to db.execute() 
db.execute("INSERT INTO json_col VALUES (" + json_value + ")") 

बेहतर, उपयोग:

# Correct method. Uses parameter/bind variables. 
# Notice that you are passing in 2 arguments to db.execute() 
db.execute("INSERT INTO json_col VALUES %s", json_value) 

आशा इस मदद करता है। यदि हां, तो मुझे बताएं। :-)

यदि आपको अभी भी कोई समस्या है, तो हमें आपके वाक्यविन्यास की अधिक बारीकी से जांच करने की आवश्यकता होगी।

+0

@Aran क्या मेरा उत्तर मदद मिली? अगर ऐसा है तो वोट दें और उत्तर दें .. धन्यवाद। :) – nonot1

+0

मैंने अभी अपना कोड अपलोड कर लिया है और अभी भी त्रुटियां हो रही हैं जो SQL क्वेरी लिखने के विभिन्न तरीकों का प्रयास कर रही है ... – Aran

0

आप वास्तविक एसक्यूएल स्ट्रिंग पर एक नज़र प्राप्त करने की आवश्यकता, कुछ इस तरह का प्रयास करें:

sqlstr = "INSERT INTO tweets_unprocessed (text, created_at, twitter_id, user_id, user_screen_name, json) VALUES (%s,%s,%s,%s,%s,%s)", (status.get('text'), status.get('created_at'), status.get('id'), status.get('user', {}).get('id'), status.get('user', {}).get('screen_name'), status) 
print "about to execute(%s)" % sqlstr 
twitdb.execute(sqlstr) 

मैं कल्पना तुम वहाँ में कुछ आवारा उद्धरण, कोष्ठक या कोष्ठक को खोजने के लिए जा रहे हैं।

+0

ठीक है अगर मेरे पास भटक उद्धरण, ब्रैकेट या कंस्ट्रैसिस है - मैं एसक्यूएल के साथ उपयोग के लिए स्ट्रिंग कैसे तैयार कर सकता हूं क्या पाइथन डीबी-एपीआई में कोई फ़ंक्शन है? – Aran

+0

एसक्यूएल सिंटैक्स में एक त्रुटि के लिए, "एसक्यूएल के साथ उपयोग के लिए स्ट्रिंग तैयार करना" का मतलब वाक्यविन्यास को ठीक करना होगा ... शायद इसके लिए एपीआई कॉल नहीं है :) "निष्पादित करने के बारे में ..." से आउटपुट पोस्ट करें बयान। – qneill

1
@route('/shoes', method='POST') 
def createorder(): 
    cursor = db.cursor() 
    data = request.json 
    p_id = request.json['product_id'] 
    p_desc = request.json['product_desc'] 
    color = request.json['color'] 
    price = request.json['price'] 
    p_name = request.json['product_name'] 
    q = request.json['quantity'] 
    createDate = datetime.now().isoformat() 
    print (createDate) 
    response.content_type = 'application/json' 
    print(data) 
    if not data: 
     abort(400, 'No data received') 

    sql = "insert into productshoes (product_id, product_desc, color, price, product_name,   quantity, createDate) values ('%s', '%s','%s','%d','%s','%d', '%s')" %(p_id, p_desc, color, price, p_name, q, createDate) 
    print (sql) 
    try: 
    # Execute dml and commit changes 
     cursor.execute(sql,data) 
     db.commit() 
     cursor.close()   
    except: 
    # Rollback changes 
     db.rollback() 
    return dumps(("OK"),default=json_util.default)