2010-11-22 85 views
11

我在Python中有一個JSON對象。我正在使用Python DB-API和SimpleJson。我正在嘗試將json插入到MySQL表中。使用Python將JSON插入到MySQL中

目前我得到的錯誤,我相信這是由於JSON對象中的單引號''。

如何使用Python將我的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 2010-11-22 23:14:03

+0

您使用什麼代碼實際將JSON數據插入到數據庫中? – 2010-11-22 23:52:03

+1

希望是一個愚蠢的問題,但你(希望)使用綁定變量? – Gerrat 2010-11-22 23:56:11

回答

0

的錯誤可能是由於您嘗試插入json的字段的大小溢出所致。沒有任何代碼,很難幫助你。

你有沒有考慮一個非sql數據庫系統,如couchdb,這是一個依賴於json格式的面向文檔的數據庫?

1

您應該能夠插入intyo文本或BLOB列容易

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

爲了擴大對其他答案:

基本上你需要做的兩件事情肯定:

  1. 您有足夠的空間存儲您想要放入的字段中的全部數據。不同的數據庫字段類型可以適合不同數量的數據。 參見:MySQL String Datatypes。您可能需要「TEXT」或「BLOB」類型。

  2. 你安全地將數據傳遞給數據庫。傳遞數據的一些方法可能會導致數據庫「查看」數據,如果數據看起來像SQL,它會感到困惑。這也是一個安全風險。請參閱:SQL Injection

的解決方案#1是檢查數據庫設計有正確的字段類型。

#2的解決方案是使用參數化(綁定)查詢。例如,而不是:

​​

更好,用途:

# 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

你需要得到看看實際的SQL字符串,嘗試這樣的事情:

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) 

我想你會發現一些流浪報價,括號或括號在那裏。

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)