Link to home
Start Free TrialLog in
Avatar of Sanjay Gandhi
Sanjay GandhiFlag for India

asked on

Python Code

Hello,

Attached is the Python code.  In the attachment:

Number 1 indicates that data was extracted giving appropriate command, and the displayed correctly.
A indicates the command written, and similar commands are given below as well.
Numbers 2 and 3 indicate response from system.

What is this response from system, (sqlite3.Cursor object at 0x03FC10A0), and what is missed over here? What should be expected as a result? There are similar commands below also.

Thanks,
San.
Avatar of Norie
Norie

Sanjay

There's no attachment I'm afraid.
Without code it is hard to tell...
sqlite3.Cursor object at 0x03FC10A0 : Means there is a sqlite3 object. with a subtype of Cursor.

Searching gives: https://docs.python.org/2/library/sqlite3.html

Say this object is call xconn & xcur

xconn = sqlite3.connect(...)
xcur = xconn.cursor()  
print(xcur)    # this is shown in the above line "sqlite3.Cursor object at 0x03FC10A0"

xcur.execute( ... )   # would run a SQL statement
result = xcur.fetchone()... # to obtain a select result..

See referenced link for examples.
Avatar of Sanjay Gandhi

ASKER

Hi,

Thanks. Here it is.

San.
Query.jpg
Please show the statement where you instantiate the cur variable
So you say: cur.execute()....
which is evaluated as a function, Interactive python has the courtesy of printing the returned value from a function.

That why   cur.execute witout an assignment will print as the Object...
(The obect misses a print format function to pretty print it).

any x=cur.execute() ...
will prevent interactive python from printing a result.  (see the line with course_id=cur.execute(...)
Hi,

Please find attached the code, which my cousin's daughter is practicing, but does not know how to handle the lines given. I've added three comment lines after which the error (or alert) is appearing. The comment lines start from # Help.

I am attaching the Python code, and data file she is using.

Request you to please guide what example can be given in those lines. There are some question marks, and as informed the alert from system shows: (sqlite3.Cursor object at 0x03FC10A0).

Warm regards,

San.
roster.py
roster_data.json
import json
import sqlite3

conn = sqlite3.connect('rosterdb.sqlite')
cur = conn.cursor()

# Do some setup
result = cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Member;
DROP TABLE IF EXISTS Course;

CREATE TABLE User (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name   TEXT UNIQUE
);

CREATE TABLE Course (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    title  TEXT UNIQUE
);

CREATE TABLE Member (
    user_id     INTEGER,
    course_id   INTEGER,
    role        INTEGER,
    PRIMARY KEY (user_id, course_id)
)
''')

fname = input('Enter file name: ')
if len(fname) < 1:
    fname = 'roster_data_sample.json'

# [
#   [ "Charley", "si110", 1 ],
#   [ "Mea", "si110", 0 ],

str_data = open(fname).read()
json_data = json.loads(str_data)

for entry in json_data:

    name = entry[0];
    title = entry[1];

    print((name, title))

    # Help: the output till above lines is coming fine
    # what should be done in following lines to make it work fine
    # what is the coder missing

    result = cur.execute('''INSERT OR IGNORE INTO User (name)
        VALUES ( ? )''', ( name, ) )
    result = cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
    user_id = cur.fetchone()[0]

    result = cur.execute('''INSERT OR IGNORE INTO Course (title)
        VALUES ( ? )''', ( title, ) )
    result = cur.execute('SELECT id FROM Course WHERE title = ? ', (title, ))
    course_id = cur.fetchone()[0]

    result = cur.execute('''INSERT OR REPLACE INTO Member
        (user_id, course_id) VALUES ( ?, ? )''',
        ( user_id, course_id ) )

    conn.commit()

Open in new window

The result = insertions will swallow the  output of the object print.

Als better save this python as a script and run it using:
python roster.py

The output you gave is NOT an error alert. It is what interactive used python does when a variable or function is given as command.
It will print the contents. The sqlite3 object has no pretty printing attributes so you see the object reference that is created by the function cur.execute.
Converting the command from a functioncall to a n assignment x = functioncall... there is no attempt to print the contents of the function return value.
Hi, Ok.

1) cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''', ( name, ) )
and
2) cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))

A)  Why are there three ''' in line 1), and one ' in line 2) above

B) Why do these lines end with (name,). What is the meaning having nothing after name,
There is no difference... well there is but it si read as:
  ''' verions is:   ''   is empty string concatenated to 'INSERT OR IGNORE INTO User (name) VALUES ( ? )' and also concatenated with '' (another empty string).
En reality no difference.  Maybe it was aan attempt to create a comment?
Hi,

Unfortunately I am not able to understand this code. Can someone please explain me the intended meaning of this command. What this command is supposed to do. And for that intended purpose, is this command OK.

cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''', ( name, ) )

Hope I am not asking too much. Once this command is clear, I can head towards giving it right values.
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks So Much. Delighted.
Gives me proper inputs that I needed. Now I have direction.

Thanks again.