Beginner question!

Discussion in 'Python' started by SMALLp, Dec 21, 2007.

  1. SMALLp

    SMALLp Guest

    Hy! I have error something like this

    TypeError: unbound method insert() must be called with insertData
    instance as first argument (got str instance instead)

    CODE:

    File1.py
    sql.insertData.insert("files", data)

    sql.py

    class insertData:
    def insert(self, dataTable, data):
    conn = self.openConnection.openConnection()
    cursor = conn.cursor()
    sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    "+data
    cursor.execute(sql)
    conn.Close()


    Help and advice neaded!
     
    SMALLp, Dec 21, 2007
    #1
    1. Advertising

  2. SMALLp

    Guest

    On Dec 21, 9:11 am, SMALLp <-com.hr> wrote:
    > Hy! I have error something like this
    >
    > TypeError: unbound method insert() must be called with insertData
    > instance as first argument (got str instance instead)
    >
    > CODE:
    >
    > File1.py
    > sql.insertData.insert("files", data)
    >
    > sql.py
    >
    > class insertData:
    > def insert(self, dataTable, data):
    > conn = self.openConnection.openConnection()
    > cursor = conn.cursor()
    > sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    > file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    > uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    > "+data
    > cursor.execute(sql)
    > conn.Close()
    >
    > Help and advice neaded!


    I think you need to post the real traceback or the real code since
    your error message doesn't look like it has anything to do with the
    code above. At least, I do not see a method named "insert".

    Which database module are you using?

    Mike
     
    , Dec 21, 2007
    #2
    1. Advertising

  3. SMALLp

    SMALLp Guest

    wrote:
    > On Dec 21, 9:11 am, SMALLp <-com.hr> wrote:
    >> Hy! I have error something like this
    >>
    >> TypeError: unbound method insert() must be called with insertData
    >> instance as first argument (got str instance instead)
    >>
    >> CODE:
    >>
    >> File1.py
    >> sql.insertData.insert("files", data)
    >>
    >> sql.py
    >>
    >> class insertData:
    >> def insert(self, dataTable, data):
    >> conn = self.openConnection.openConnection()
    >> cursor = conn.cursor()
    >> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    >> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    >> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    >> "+data
    >> cursor.execute(sql)
    >> conn.Close()
    >>
    >> Help and advice neaded!

    >
    > I think you need to post the real traceback or the real code since
    > your error message doesn't look like it has anything to do with the
    > code above. At least, I do not see a method named "insert".
    >
    > Which database module are you using?
    >
    > Mike


    Traceback (most recent call last):
    File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
    self.scanDirsAndFiles(dirPath)
    File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
    scanDirsAndFiles
    sql.insertData.insert("files", data)
    TypeError: unbound method insert() must be called with insertData
    instance as first argument (got str instance instead)



    share.py

    import wx
    import os
    import sql
    import login
    class sharePanel(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    sizer = wx.BoxSizer(wx.VERTICAL)

    nb = wx.Notebook(self, -1,style=wx.NB_TOP)

    self.sheet1 = p1(nb, id)
    self.sheet2 = p2(nb, id)
    self.sheet3 = p3(nb, id)

    nb.AddPage(self.sheet1, 'SEt1')
    nb.AddPage(self.sheet2, 'Sheet2')
    nb.AddPage(self.sheet3, 'Sheet3')

    self.sheet1.SetFocus()

    sizer.Add(nb,0, wx.EXPAND | wx.TOP, 20)



    self.SetSizerAndFit(sizer)

    class pageMenu(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    self.SetBackgroundColour("WHITE")

    sizer = wx.BoxSizer(wx.HORIZONTAL)
    self.links = wx.StaticText(self, -1, "<<< 1 2 3 4 5 6 7 8 9
    10 >>>")
    sizer.Add(self.links)

    class p1(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id, size=(500, 300))
    self.SetBackgroundColour("WHITE")

    sizer = wx.BoxSizer(wx.VERTICAL)

    self.pMenu = pageMenu(self, id)
    sizer.Add(self.pMenu,0, wx.LEFT | wx.TOP | wx.RIGHT, 40)

    fileList = myFilesList(self, id)
    sizer.Add(fileList, 0, wx.LEFT | wx.RIGHT, 10)

    shareBox = sharePanelBox(self, id)
    sizer.Add(shareBox,0, wx.EXPAND | wx.TOP | wx.LEFT, 20)

    sizer.Fit(self)
    self.SetSizerAndFit(sizer)

    class p2(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id, size=(200, 200))
    self.SetBackgroundColour("RED")

    class p3(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id, size=(200, 100))
    self.SetBackgroundColour("YELLOW")


    class myFilesList(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    self.SetBackgroundColour("WHITE")

    vsizer = wx.BoxSizer(wx.VERTICAL)
    data = {'fileName':'My filename',
    'filePath':'/home/pofuk/Documents/myfile', 'fileSize':'41223 MB'}


    for i in range(10):
    d = [myFilesListItem(self, id, data)]
    vsizer.Add(d[0],1, wx.EXPAND)

    self.SetSizerAndFit(vsizer)



    class myFilesListItem(wx.Panel):
    def __init__(self, parent, id, data):
    wx.Panel.__init__(self, parent, id)
    self.SetBackgroundColour("WHITE")

    hsizer = wx.BoxSizer(wx.HORIZONTAL)

    self.fileName = wx.TextCtrl(self, -1, data['fileName'])
    hsizer.Add(self.fileName,1, wx.EXPAND)
    self.filePath = wx.StaticText(self, -1, data['filePath'])
    hsizer.Add(self.filePath,1, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 10)
    self.fileSize = wx.StaticText(self, -1, data['fileSize'])
    hsizer.Add(self.fileSize, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 30)

    self.delete = wx.Button(self, -1, "Delete")
    hsizer.Add(self.delete, 0, wx.LEFT, 20)

    self.SetSizerAndFit(hsizer)

    def setData(text):
    self.fileName.Value = text


    class sharePanelBox(wx.Panel):
    def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)
    self.SetBackgroundColour("WHITE")
    sizer = wx.FlexGridSizer(1,3)

    self.files = wx.CheckBox(self, -1, 'Share Files')
    self.folders = wx.CheckBox(self, -1, 'Share Folders')
    shareButton = wx.Button(self, -1, 'Share')

    sizer.Add(self.files,0, wx.ALIGN_CENTER_VERTICAL)
    sizer.Add(self.folders,0, wx.ALIGN_CENTER_VERTICAL)
    sizer.Add(shareButton, 0, wx.LEFT, 20)

    self.Bind(wx.EVT_BUTTON, self.share, id=shareButton.GetId())
    self.SetSizerAndFit(sizer)

    def share(self, event):
    if self.files.IsChecked() and self.folders.IsChecked():
    dialog = wx.DirDialog(self)
    dialog.ShowModal()
    dirPath = dialog.GetPath()
    print dirPath
    self.scanDirsAndFiles(dirPath)
    if self.files.IsChecked() and not self.folders.IsChecked():
    dialog = wx.FileDialog(self, style=wx.OPEN)
    dialog.ShowModal()
    if not self.files.IsChecked() and self.folders.IsChecked():
    dialog = wx.DirDialog(self)
    dialog.ShowModal()

    def scanDirsAndFiles(self, dirPath):
    for item in os.listdir(dirPath):
    if os.path.isdir(os.path.join(dirPath, item)):
    scanDirsAndFiles(os.path.join(dirPath, item))
    if os.path.isfile(os.path.join(dirPath, item)):
    user_name = login.getUserName()
    fileName = item
    fileSize = os.path.getsize(os.path.join(dirPath, item))
    filePathLocal = os.path.join(dirPath, item)
    filePathFTP = ""
    currentLocation = "Local"
    FTP_valid_time = 7
    uploaded = ""
    lastModified = "NOW()"
    lastVerified = "NOW()"
    fileType = "file"
    fileCategory = "Ostalo"

    data = [fileName, fileSize, filePathLocal, filePathFTP,
    currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
    fileType, fileCategory]

    sql.insertData.insert("files", data)




    sql.py


    connectionString = login.initialize()

    class openConnection:
    def openConnection(self):
    conn = mysql.connect(host=connectionString["host"],
    user=connectionString["user"], passwd=connectionString["passwd"],
    db=connectionString["db"])
    return conn


    class getData:


    def select(self, dataTable, limitOffset, count):
    conn = self.openConnection.openConnection()
    cursor = conn.cursor()
    sql = "SELECT * FROM " + dataTable + " LIMIT " + limitOffset +","+ count
    print sql
    cursor.execute(sql)
    result = []
    for i in range(int(count)):
    result.append(cursor.fetchone())

    conn.close()
    return result
    def select(self, dataTable, limitOffset, count, filterCol, filterValue):
    conn = self.openConnection()
    cursor = conn.cursor()
    sql = "SELECT FROM " + dataTable + " WHERE
    "+filterCol+"="+filerValue+" LIMIT "+limitOffset+","+count
    print sql
    cursor.execute(sql)
    result = []
    for i in range(int(count)):
    result.append(cursor.fetchone())
    conn.close()

    class insertData:
    def insert(self, dataTable, data):
    conn = self.openConnection.openConnection()
    cursor = conn.cursor()
    sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    "+data
    cursor.execute(sql)
    conn.Close()
     
    SMALLp, Dec 21, 2007
    #3
  4. SMALLp

    Chris Mellon Guest

    On Dec 21, 2007 9:11 AM, SMALLp <-com.hr> wrote:
    > Hy! I have error something like this
    >
    > TypeError: unbound method insert() must be called with insertData
    > instance as first argument (got str instance instead)
    >
    > CODE:
    >
    > File1.py
    > sql.insertData.insert("files", data)
    >
    > sql.py
    >
    > class insertData:
    > def insert(self, dataTable, data):
    > conn = self.openConnection.openConnection()
    > cursor = conn.cursor()
    > sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    > file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    > uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    > "+data
    > cursor.execute(sql)
    > conn.Close()
    >
    >
    > Help and advice neaded!
    > --


    You are unclear on the distinction between classes and instances of
    those classes. Following Python convention and naming your classes in
    CapsCase (InsertData not insertData) would help. I recommend taking 2
    giant steps backward and working through the python tutorial and dive
    into python, then coming back to this project.
     
    Chris Mellon, Dec 21, 2007
    #4
  5. SMALLp

    Guest


    > Traceback (most recent call last):
    > File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
    > self.scanDirsAndFiles(dirPath)
    > File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
    > scanDirsAndFiles
    > sql.insertData.insert("files", data)
    > TypeError: unbound method insert() must be called with insertData
    > instance as first argument (got str instance instead)
    >
    > share.py



    <snip>


    > def scanDirsAndFiles(self, dirPath):
    > for item in os.listdir(dirPath):
    > if os.path.isdir(os.path.join(dirPath, item)):
    > scanDirsAndFiles(os.path.join(dirPath, item))
    > if os.path.isfile(os.path.join(dirPath, item)):
    > user_name = login.getUserName()
    > fileName = item
    > fileSize = os.path.getsize(os.path.join(dirPath, item))
    > filePathLocal = os.path.join(dirPath, item)
    > filePathFTP = ""
    > currentLocation = "Local"
    > FTP_valid_time = 7
    > uploaded = ""
    > lastModified = "NOW()"
    > lastVerified = "NOW()"
    > fileType = "file"
    > fileCategory = "Ostalo"
    >
    > data = [fileName, fileSize, filePathLocal, filePathFTP,
    > currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
    > fileType, fileCategory]
    >
    > sql.insertData.insert("files", data)
    >



    <snip>


    > class insertData:
    > def insert(self, dataTable, data):
    > conn = self.openConnection.openConnection()
    > cursor = conn.cursor()
    > sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    > file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    > uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    > "+data
    > cursor.execute(sql)
    > conn.Close()


    It doesn't look like you are instantiating the insertData class. You
    would need to do something like:

    # untested
    foo = insertData()
    foo.insert("files", data)


    But I agree with Chris. You really do need to go through a tutorial on
    using classes and following Python naming conventions. Dive Into
    Python and some of the other online resources are very helpful.

    This is something that I have trouble with myself since wxPython uses
    CamelCase for classes and methods/functions and and most
    recommendations for plain Python seem to only want CamelCase for
    classes and something like myFunct or myMethod for the other objects.

    Mike
     
    , Dec 21, 2007
    #5
  6. SMALLp

    SMALLp Guest

    wrote:
    >> Traceback (most recent call last):
    >> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
    >> self.scanDirsAndFiles(dirPath)
    >> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
    >> scanDirsAndFiles
    >> sql.insertData.insert("files", data)
    >> TypeError: unbound method insert() must be called with insertData
    >> instance as first argument (got str instance instead)
    >>
    >> share.py

    >
    >
    > <snip>
    >
    >
    >> def scanDirsAndFiles(self, dirPath):
    >> for item in os.listdir(dirPath):
    >> if os.path.isdir(os.path.join(dirPath, item)):
    >> scanDirsAndFiles(os.path.join(dirPath, item))
    >> if os.path.isfile(os.path.join(dirPath, item)):
    >> user_name = login.getUserName()
    >> fileName = item
    >> fileSize = os.path.getsize(os.path.join(dirPath, item))
    >> filePathLocal = os.path.join(dirPath, item)
    >> filePathFTP = ""
    >> currentLocation = "Local"
    >> FTP_valid_time = 7
    >> uploaded = ""
    >> lastModified = "NOW()"
    >> lastVerified = "NOW()"
    >> fileType = "file"
    >> fileCategory = "Ostalo"
    >>
    >> data = [fileName, fileSize, filePathLocal, filePathFTP,
    >> currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
    >> fileType, fileCategory]
    >>
    >> sql.insertData.insert("files", data)
    >>

    >
    >
    > <snip>
    >
    >
    >> class insertData:
    >> def insert(self, dataTable, data):
    >> conn = self.openConnection.openConnection()
    >> cursor = conn.cursor()
    >> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    >> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    >> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    >> "+data
    >> cursor.execute(sql)
    >> conn.Close()

    >
    > It doesn't look like you are instantiating the insertData class. You
    > would need to do something like:
    >
    > # untested
    > foo = insertData()
    > foo.insert("files", data)
    >
    >
    > But I agree with Chris. You really do need to go through a tutorial on
    > using classes and following Python naming conventions. Dive Into
    > Python and some of the other online resources are very helpful.
    >
    > This is something that I have trouble with myself since wxPython uses
    > CamelCase for classes and methods/functions and and most
    > recommendations for plain Python seem to only want CamelCase for
    > classes and something like myFunct or myMethod for the other objects.
    >
    > Mike

    Thanks! I solved the problem. And I thing i understand now.
     
    SMALLp, Dec 21, 2007
    #6
  7. On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
    > >> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    > >> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    > >> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    > >> "+data
    > >> cursor.execute(sql)


    > Thanks! I solved the problem. And I thing i understand now.


    You may have solved your initial problem, but the above snippet raises
    two red flags:

    1) Why is the table name coming from a variable? This implies to me that
    you a working with a collection of tables with different names that all
    have the same column names. If that is the case, that smells of really
    bad database design. If at all possible, those tables should be merged
    into one table that has an additional column (or set of columns) for
    distinguishing which "fragment" each row is in.

    2) Sticking literal values into an SQL query string is a bad idea. You
    should learn about parametrized queries, e.g. here:
    http://informixdb.blogspot.com/2007/07/filling-in-blanks.html

    Hope this helps,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, Dec 21, 2007
    #7
  8. SMALLp

    SMALLp Guest

    Carsten Haese wrote:
    > On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
    >>>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    >>>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    >>>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    >>>> "+data
    >>>> cursor.execute(sql)

    >
    >> Thanks! I solved the problem. And I thing i understand now.

    >
    > You may have solved your initial problem, but the above snippet raises
    > two red flags:
    >
    > 1) Why is the table name coming from a variable? This implies to me that
    > you a working with a collection of tables with different names that all
    > have the same column names. If that is the case, that smells of really
    > bad database design. If at all possible, those tables should be merged
    > into one table that has an additional column (or set of columns) for
    > distinguishing which "fragment" each row is in.
    >
    > 2) Sticking literal values into an SQL query string is a bad idea. You
    > should learn about parametrized queries, e.g. here:
    > http://informixdb.blogspot.com/2007/07/filling-in-blanks.html
    >
    > Hope this helps,
    >

    Good question. I'm using only one tale and have no idea why i had table
    name from variable. But every new knowledge comes handy.

    One more question. How does my code looks like. I couldn't find any open
    source program written in python to learn from, so i read some tutorials
    and I'm not sure about how it looks.
     
    SMALLp, Dec 21, 2007
    #8
  9. SMALLp

    Guest

    On Dec 21, 1:44 pm, SMALLp <-com.hr> wrote:
    > Carsten Haese wrote:
    > > On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
    > >>>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
    > >>>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
    > >>>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
    > >>>> "+data
    > >>>> cursor.execute(sql)

    >
    > >> Thanks! I solved the problem. And I thing i understand now.

    >
    > > You may have solved your initial problem, but the above snippet raises
    > > two red flags:

    >
    > > 1) Why is the table name coming from a variable? This implies to me that
    > > you a working with a collection of tables with different names that all
    > > have the same column names. If that is the case, that smells of really
    > > bad database design. If at all possible, those tables should be merged
    > > into one table that has an additional column (or set of columns) for
    > > distinguishing which "fragment" each row is in.

    >
    > > 2) Sticking literal values into an SQL query string is a bad idea. You
    > > should learn about parametrized queries, e.g. here:
    > >http://informixdb.blogspot.com/2007/07/filling-in-blanks.html

    >
    > > Hope this helps,

    >
    > Good question. I'm using only one tale and have no idea why i had table
    > name from variable. But every new knowledge comes handy.
    >
    > One more question. How does my code looks like. I couldn't find any open
    > source program written in python to learn from, so i read some tutorials
    > and I'm not sure about how it looks.


    You couldn't find any programs written in Python? What the!?

    Here's a few:

    http://cheeseshop.python.org/pypi/UliPad/3.6/
    http://spambayes.sourceforge.net/
    http://sourceforge.net/softwaremap/trove_list.php?form_cat=178

    Mike
     
    , Dec 21, 2007
    #9
  10. a écrit :
    > On Dec 21, 9:11 am, SMALLp <-com.hr> wrote:
    >

    (snip)
    >>class insertData:
    >> def insert(self, dataTable, data):

    (snip)
    >
    > I think you need to post the real traceback or the real code since
    > your error message doesn't look like it has anything to do with the
    > code above. At least, I do not see a method named "insert".


    May I suggest a new pair of glasses ?-)
     
    Bruno Desthuilliers, Dec 21, 2007
    #10
  11. SMALLp a écrit :
    (snip)
    > One more question. How does my code looks like. I couldn't find any open
    > source program written in python


    You must be jocking ?
     
    Bruno Desthuilliers, Dec 21, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerker Hammarberg

    Beginner question: What trigs processes

    Jerker Hammarberg, Jul 17, 2003, in forum: VHDL
    Replies:
    16
    Views:
    1,648
    Mike Treseler
    Jul 22, 2003
  2. smu
    Replies:
    3
    Views:
    2,656
    shobhit24
    Jun 28, 2006
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    637
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    1,024
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    249
    7stud --
    Apr 12, 2011
Loading...

Share This Page