简体   繁体   中英

Storing a python dictionary in a Mysql Database

How should you frame an SQL queries for inserting and retrieving python dictionaries from MySQL databases?

The parsed_tweets is an example of a dicctionary I'd like to be able to store.

for tweet in twtsearch.search_tweets_iterable(twtsearchorder):
    if tweet_limit > 0 :
        parsed_tweets['userid'] = tweet['user']['id']
        parsed_tweets['uname'] = tweet['user']['screen_name']
        parsed_tweets['content'] = tweet['text']
        parsed_tweets['location'] = tweet['user']['location']
        parsed_tweets['retweets'] = tweet['retweet_count']
        parsed_tweets['favs'] = tweet['favorite_count']

Unfortunately, the best you can do is store the JSON as a text in mySQL because mySQL doesn't support JSON format. You can do this by importing json and calling the dump method to convert the JSON to text. Similarly, you can call the load method to parse the json back into your program.

If you need to store a json into a DB, try using couchDB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM