eryckop
asked on
AttributeError: 'str' object has no attribute 'student_id'
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\prototype1.py ", line 112, in -toplevel-
main()
File "C:\Python24\prototype1.py ", line 109, in main
surgery.menudisplay()
File "C:\Python24\prototype1.py ", line 98, in menudisplay
self.addstudent() # later development, check that the student is not already in the database
File "C:\Python24\prototype1.py ", line 68, in addstudent
databasemodule.insertStude nt(self.ge tSID(), self.email, 's')
File "C:\Python24\databasemodul e.py", 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
self.email = email
cursor.execute('INSERT INTO Students VALUES(?, ?)',(self.student_id, self.email))
#def insertnew(self, ai, aem):
# self.s_id = s_id
# self.email = email
# cursor.execute('INSERT INTO Students VALUES(?, ?)',(ai, aem))
def insertLecturer(self, l_id, email):
self.l_id = l_id
self.email = email
cusor.execute('INSERT INTO Lecturer VALUES(?, ?)', (self.l_id, self.email))
def insertSlot(self, slotid, time, date, s_id, l_id):
self.slotid = slotid
self.time = time
self.date = date
self.s_id = s_id
self.l_id = l_id
cursor.execute('INSERT INTO Slot VALUES(?, ?, ?, ?, ?)',(self.slotid, self.time, self.date, 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):
try:
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]
self.setTitle(keyword)
l_id = split(line, ' ')[1]
self.setLID(l_id)
u_id = split(line, ' ')[2]
self.setSID(u_id)
detail = split(line, ' ')[3] + ' ' + split(line, ' ')[4]
self.setMessage(detail)
print "Title : ", self.getTitle(), "name : ", self.getLID(), "Student ID : ", self.getSID(), "message : ", self.getMessage()
myfile.close()
# write into the database what has been read from the file
def addstudent(self):
print (self.getSID())
self.email = self.getSID() + "@kingston.ac.uk"
databasemodule.insertStude nt(self.ge tSID(), self.email, 's')
#mydbmodule.insertStudent( self.getSI D(), self.email)
def addlecturer(self):
self.email = self.getLID() + "@kingston.ac.uk"
databasemodule.insertLectu rer(self.g etSID(), self.email, 'l')
#databasemodule.databasemo dule.inser tStudent(s elf.getSID (), email)
#menu display
def menudisplay(self):
# s = raw_input('create database : ')
# if s=='yes':
#databasemodule.createStud ent()
# databasemodule.createLectu rer()
try:
file_input = raw_input('Read from File? yes/no : ')
if file_input =='yes':
self.lineread()
else:
print('No Action Taken, Thank You')
#break
except TypeError:
print "Input not acceptable!"
try:
input = raw_input('update database? yes/no : ')
if input =='yes':
self.addstudent() # later development, check that the student is not already in the database
self.addlecturer()
else:
print("the database has not been updated")
#break
except TypeError:
print "Input not acceptable!"
def main():
surgery = Surgery('','','','')
surgery.menudisplay()
main()
.......................... ..........
Your help and advice is much appreciated.
Kind Regards
Eric
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\prototype1.py
main()
File "C:\Python24\prototype1.py
surgery.menudisplay()
File "C:\Python24\prototype1.py
self.addstudent() # later development, check that the student is not already in the database
File "C:\Python24\prototype1.py
databasemodule.insertStude
File "C:\Python24\databasemodul
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
self.email = email
cursor.execute('INSERT INTO Students VALUES(?, ?)',(self.student_id, self.email))
#def insertnew(self, ai, aem):
# self.s_id = s_id
# self.email = email
# cursor.execute('INSERT INTO Students VALUES(?, ?)',(ai, aem))
def insertLecturer(self, l_id, email):
self.l_id = l_id
self.email = email
cusor.execute('INSERT INTO Lecturer VALUES(?, ?)', (self.l_id, self.email))
def insertSlot(self, slotid, time, date, s_id, l_id):
self.slotid = slotid
self.time = time
self.date = date
self.s_id = s_id
self.l_id = l_id
cursor.execute('INSERT INTO Slot VALUES(?, ?, ?, ?, ?)',(self.slotid, self.time, self.date, 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):
try:
myfile = open('studentmessage.txt',
except IOError:
print "The file you are trying to open does not exist, Bye!"
for line in myfile.readlines():
keyword = split(line, ' ')[0]
self.setTitle(keyword)
l_id = split(line, ' ')[1]
self.setLID(l_id)
u_id = split(line, ' ')[2]
self.setSID(u_id)
detail = split(line, ' ')[3] + ' ' + split(line, ' ')[4]
self.setMessage(detail)
print "Title : ", self.getTitle(), "name : ", self.getLID(), "Student ID : ", self.getSID(), "message : ", self.getMessage()
myfile.close()
# write into the database what has been read from the file
def addstudent(self):
print (self.getSID())
self.email = self.getSID() + "@kingston.ac.uk"
databasemodule.insertStude
#mydbmodule.insertStudent(
def addlecturer(self):
self.email = self.getLID() + "@kingston.ac.uk"
databasemodule.insertLectu
#databasemodule.databasemo
#menu display
def menudisplay(self):
# s = raw_input('create database : ')
# if s=='yes':
#databasemodule.createStud
# databasemodule.createLectu
try:
file_input = raw_input('Read from File? yes/no : ')
if file_input =='yes':
self.lineread()
else:
print('No Action Taken, Thank You')
#break
except TypeError:
print "Input not acceptable!"
try:
input = raw_input('update database? yes/no : ')
if input =='yes':
self.addstudent() # later development, check that the student is not already in the database
self.addlecturer()
else:
print("the database has not been updated")
#break
except TypeError:
print "Input not acceptable!"
def main():
surgery = Surgery('','','','')
surgery.menudisplay()
main()
..........................
Your help and advice is much appreciated.
Kind Regards
Eric
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks once a again.
Regards,
Eric