AttributeError: 'str' object has no attribute 'student_id'

Posted on 2006-07-20
Last Modified: 2013-11-19
Hi All,
Sorry if a similar question has been asked before and answered.

I am trying to write a piece that will read from a file and populate the file content into various tables in a database.

I started of in creating a module for creating my database tables and the insert statements as well,  The main program starts of nicely to read from the file after input 'yes' at the prompt to read from the file, and the file content printed to indicate that the file has been read successfully.

Now when I get to the bit that I want to insert into a database table with the content read from the file, I get the Error:


Traceback (most recent call last):
  File "C:\Python24\", line 112, in -toplevel-
  File "C:\Python24\", line 109, in main
  File "C:\Python24\", line 98, in menudisplay
    self.addstudent() # later development, check that the student is not already in the database
  File "C:\Python24\", line 68, in addstudent
    databasemodule.insertStudent(self.getSID(),, 's')
  File "C:\Python24\", line 20, in insertStudent
    self.student_id = 'k0212653'
AttributeError: 'str' object has no attribute 'student_id'

please find my actual codes below as well.
Module Database Code:
global connection
connection = sqlite.connect('Surgery.db')
global cursor
cursor = connection.cursor()

    #declaring methods for the databases and the statements needed
def createStudent():
    cursor.execute('CREATE TABLE Students(Student_ID VARCHAR(8) PRIMARY KEY, EmailAddress VARCHAR(50))')

def createLecturer():
    cursor.execute('CREATE TABLE Lecturer(Lecturer_ID VARCHAR(8) PRIMARY KEY, EmailAddress VARCHAR(50))')
def createSlot(self):
    cursor.execute('CREATE TABLE Slot(Slot_ID VARCHAR(6) PRIMARY KEY, Time DateTime, Date DateTime, StudentID NOT NULL REFERENCES "Studnets"("Student_ID"), id NOT NULL REFERENCES "Lecture"("Lecturer_ID"))')

def insertStudent(self, student_id, email):
    self.student_id = student_id = email
    cursor.execute('INSERT INTO Students VALUES(?, ?)',(self.student_id,
#def insertnew(self, ai, aem):
#    self.s_id = s_id
# = email
#    cursor.execute('INSERT INTO Students VALUES(?, ?)',(ai, aem))

def insertLecturer(self, l_id, email):
    self.l_id = l_id = email
    cusor.execute('INSERT INTO Lecturer VALUES(?, ?)', (self.l_id,

def insertSlot(self, slotid, time, date, s_id, l_id):
    self.slotid = slotid
    self.time = time = date
    self.s_id = s_id
    self.l_id = l_id
    cursor.execute('INSERT INTO Slot VALUES(?, ?, ?, ?, ?)',(self.slotid, self.time,, self.s_id, self.l_id))

from string import split
import string
import databasemodule

class Surgery:
#    global mydbmodule
#    mydbmodule = Databases
    #global myfile
    def __init__(self, title, l_id, s_id, message ):
        self.title = title
        self.l_id = l_id
        self.s_id = s_id
        self.message = message
    def getTitle(self):
        return self.title
    def setTitle(self, value):
        self.title = value

    def getLID(self):
        return self.l_id
    def setLID(self, value):
        self.l_id = value

    def getSID(self):
        return self.s_id
    def setSID(self, value):
        self.s_id = value

    def getMessage(self):
        return self.message
    def setMessage(self, value):
        self.message = value

    # read each line and seperate the worlds  in the file
    def lineread(self):
            myfile = open('studentmessage.txt', 'r')
        except IOError:
            print "The file you are trying to open does not exist, Bye!"
        for line in myfile.readlines():
            keyword = split(line, ' ')[0]
            l_id = split(line, ' ')[1]
            u_id = split(line, ' ')[2]
            detail = split(line, ' ')[3] + ' ' + split(line, ' ')[4]
            print "Title : ", self.getTitle(), "name : ", self.getLID(), "Student ID : ", self.getSID(), "message : ", self.getMessage()


# write into the database what has been read from the file
    def addstudent(self):
        print (self.getSID())
       = self.getSID() + ""
        databasemodule.insertStudent(self.getSID(),, 's')
    def addlecturer(self): = self.getLID() + ""
        databasemodule.insertLecturer(self.getSID(),, 'l')
        #databasemodule.databasemodule.insertStudent(self.getSID(), email)

#menu display

    def menudisplay(self):
       # s = raw_input('create database : ')
       # if s=='yes':
       #     databasemodule.createLecturer()
           file_input = raw_input('Read from File? yes/no : ')
           if file_input =='yes':
               print('No Action Taken, Thank You')
       except TypeError:
           print "Input not acceptable!"
           input = raw_input('update database? yes/no : ')
           if input =='yes':
               self.addstudent() # later development, check that the student is not already in the database
               print("the database has not been updated")
       except TypeError:
           print "Input not acceptable!"
def main():
    surgery = Surgery('','','','')


Your help and advice is much appreciated.

Kind Regards

Question by:eryckop
LVL 14

Accepted Solution

RichieHindle earned 250 total points
ID: 17144755
"def insertStudent(self, student_id, email):" - this is a plain function, not a method of a class, so calling the first parameter 'self' is misleading.  Then when you call it: "databasemodule.insertStudent(self.getSID(),, 's')" you pass in a string as the first parameter, but you then try to add an arbitrary attribute to that string: "self.student_id = student_id".

It looks to me as though your databasemodule code should look like this:

def insertStudent(student_id, email):
    cursor.execute('INSERT INTO Students VALUES(?, ?)',(student_id, email))

There is no 'self', and no need for one.

Author Comment

ID: 17145446
Thanks very much Richieidle, your expert advice has saved me a lot of time.

Thanks once a again.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
python sqlite question 11 46
regex to extract ip:john 17 69
Turning python script into an applet 12 104
Download a website to hdd 2 50
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now