Jackson Tom
Jackson Tom

Reputation: 7

How to check if the import of data successful or not

I write a query and I want to add the json data into Redis, but after processed(I use pycharm), there shows no results just Process finished with exit code 0. So how can I check if it is successful or not?

Here is the code:

import json
import redis
import mysql.connector

r = redis.StrictRedis(
host='localhost',
port=6379,
charset="utf-8", decode_responses=True)

cnx = mysql.connector.connect(user='root', password='000000',
                              host='127.0.0.1',
                              database='m')

player_data= [
    {'playID':'aardsda01', 'birthYear':'1981', 'birthMonth':'12', 'birthDay':'27', 'birthCountry':'USA', 'birthState':'CO',
     'nameFirst':'David', 'nameLast':'Aardsma'}
]

json_store = json.dumps(player_data)
r.set('players', json_store)
k = json.loads(r.get('players'))
player_data == k

Thank you very much!

Upvotes: 0

Views: 494

Answers (2)

erik258
erik258

Reputation: 16304

You're comparing player_data == k, but you're not doing anything with the result.

I took your code and modified it just slightly. so you could see what was happening. :

I add some print() statements so you can see whether or not the == comparison is true.

And I also add a nonzero exit if they're not true. You saw the exit 0 above; 0 is generally a successful exit. I added exit(1) just to show how, if your program exited with error, you could express that with your error code.

import json
import redis
import os

r = redis.StrictRedis( host='localhost', port=6379,
    charset="utf-8", decode_responses=True)

player_data= [
    {'playID':'aardsda01', 'birthYear':'1981', 'birthMonth':'12', 'birthDay':'27', 'birthCountry':'USA', 'birthState':'CO',
     'nameFirst':'David', 'nameLast':'Aardsma'}
]

json_store = json.dumps(player_data)
r.set('players', json_store)
k = json.loads(r.get('players'))
if player_data != k:
    print("player_data and k were not equal")
    exit(1) # exit codes other than 0 are errors
else:
    print("player_data and k were equal!")
    # as you've seen, 0 is the default return value

When I run this, I see that I get a success with your code.

/ # python t.py
player_data and k were equal!

Here's another command that might give you some ideas.

$ redis-cli get players | python -mjson.tool
[
    {
        "playID": "aardsda01",
        "birthYear": "1981",
        "birthMonth": "12",
        "birthDay": "27",
        "birthCountry": "USA",
        "birthState": "CO",
        "nameFirst": "David",
        "nameLast": "Aardsma"
    }
]

Upvotes: 1

GaryMBloom
GaryMBloom

Reputation: 5692

Assuming your indentation above is correct, you're asking about results. Your last line says player_data == k, which is some comparison, but it doesn't actually do anything, get stored, or prompt. Perhaps, instead of

player_data == k

you want to say:

print('Match is %r' % player_data == k)

which will give you some feedback as to whether the interaction was successful.

Upvotes: 2

Related Questions