woooee,

my python version is 2.6.2


tonyjv, thanks for the explanation

res = self.dbUse.readRow(getidrow)
File "/home/steph/scripts/phpy/bdd.py", line 37, in readRow
getRow = "// " + ligne[0] + " " + ligne[2] + "\n\n" + ligne[1]
TypeError: 'NoneType' object is unsubscriptable

Should you be using "res" instead of "ligne", and does ligne/res contain at least 3 elements? For any further progress, you should submit a simple, complete example for the SQL portion, as there is too much guessing and that always compounds the errors. The wx part of the program can wait until this is solved.

woooee,

res is a variable in the main file, and it's used in this method:

main.py

def OnListboxread(self, e):

    temp = e.GetString().strip()
    getidrow = temp.split(":", 1)[0].strip()
    res = self.dbUse.readRow(getidrow) # There i call readRow() methode
    self.ain.SetValue(res)

And res receives what readRow() methode returns (this one is in another file):

bdd.py

def readRow(self, idrow):

    self.c.execute("select titre, code, date from codes where id = :d_key", {"d_key" : idrow})

    ligne = self.c.fetchone()

    getRow = "// " + ligne[0] + " " + ligne[2] + "\n\n" + ligne[1]

    return getRow

The readRow() method returns the requested row of the table... although there's the error message...
Here again the error message:

Traceback (most recent call last):
File "/home/steph/scripts/phpy/main.py", line 187, in OnListboxread
res = self.dbUse.readRow(getidrow)
File "/home/steph/scripts/phpy/bdd.py", line 37, in readRow
getRow = "// " + ligne[0] + " " + ligne[2] + "\n\n" + ligne[1]
TypeError: 'NoneType' object is unsubscriptable

Here also the beginning of bdd.py:

class DbMng():

    def __init__(self)
:
        db_path = sys.path[0] + '/files/phpy.db'
        self.conn = sqlite3.connect(db_path)
        self.c = self.conn.cursor()

Cast to string...
you need to cast it to string like this...

getRow = "// " + str(ligne[0]) + " " + str(ligne[2]) + "\n\n" + str(ligne[1])

And you are ready to go
:)

Thank you richie :)

I have already tried, but the error is still the same...

getRow = "// " + ligne[0] + " " + ligne[2] + "\n\n" + ligne[1]
TypeError: 'NoneType' object is unsubscriptable

says that "ligne" is empty. You have to check for a successful lookup.

Many of us have an SQL generator in our tool box for quick and dirty apps. This is output from the generator and should help. It may require some tweaking because it is always a work in progress, but should give you some working code to start with, so run the program and take a look at the output from the tests and go from there.

import os
import sqlite3 as sqlite


##======================================================================
class DummyClass:
   def __init__( self ) :
      self.SQL_filename = './SQL_test_dbf'
      self.open_files()

   ##   END  __init__()

   ##----------------------------------------------------------------------
   def add_rec( self ) :
      val_tuple=(int(self.id), str(self.titre), str(self.code), str(self.date))
      self.cur.execute('INSERT INTO SQL_test_dbf values (?,?,?,?)', val_tuple)
      self.con.commit()

   ##   END  AddRec()

   ##----------------------------------------------------------------------
   def copy_to_struct( self, rec ) :
      self.id = rec[0]
      self.titre = rec[1]
      self.code = rec[2]
      self.date = rec[3]

   ##   END  copy_to_struct()

   ##----------------------------------------------------------------------
   def del_rec( self, value_to_delete ) :
      self.cur.execute("DELETE FROM SQL_test_dbf WHERE SQL_field=:name_dict", {"name_dict":value_to_delete})

   ##   END  del_rec()

   ##----------------------------------------------------------------------
   def list_all_recs( self ) :
      self.cur.execute("select * from SQL_test_dbf")
      recs_list = self.cur.fetchall()
      for rec in recs_list:
         print rec

   ##   END  list_all_recs

   ##----------------------------------------------------------------------
   def lookup_first_field( self, lookup_int ) :
      self.cur.execute("select * from SQL_test_dbf where id==:dic_lookup", {"dic_lookup":int(lookup_int)})
      recs_list = self.cur.fetchall()
      print
      print "lookup_first_field" 
      if len(recs_list):
         for rec in recs_list:
            self.copy_to_struct(rec)
            self.print_rec()
      else:
         print "No records found for", lookup_int

   ##   END  lookup_first_field()

   ##----------------------------------------------------------------------
   def lookup_first_2_fields( self, lookup_dic ) :
      self.cur.execute("select * from SQL_test_dbf where id==:dic_field_1 and titre==:dic_field_2", lookup_dic)

      recs_list = self.cur.fetchall()
      print
      print "lookup_first_2_fields" 
      if len(recs_list):
         for rec in recs_list:
            self.copy_to_struct(rec)
            self.print_rec()
      else:
         print "No records found"

   ##   END  lookup_first_2_field()

   ##----------------------------------------------------------------------
   def open_files( self ) :
         ##  a connection to the database file
         self.con = sqlite.connect('./SQL_test_dbf')
         # Get a Cursor object that operates in the context of Connection con
         self.cur = self.con.cursor()

         ##--- CREATE FILE ONLY IF IT DOESN'T EXIST
         self.cur.execute('''CREATE TABLE IF NOT EXISTS SQL_test_dbf(id integer primary key, titre varchar, code varchar, date varchar)''')

   ##   END  open_files()

   ##----------------------------------------------------------------------
   def print_rec( self ) :
      spaces = ""
      print spaces, "id =", self.id
      spaces = "     "
      print spaces, "titre =", self.titre
      print spaces, "code =", self.code
      print spaces, "date =", self.date

   ##   END  rec_struct()

   ##----------------------------------------------------------------------
   def rec_struct( self ) :
      self.id = ""
      self.titre = ""
      self.code = ""
      self.date = ""

   ##   END  rec_struct()

##----------------------------------------------------------------------
def test_data( class_ptr ) :
   print 'test_data --> add records'
   class_ptr.rec_struct()
   class_ptr.id = 1
   class_ptr.titre = "test_A_1"
   class_ptr.code = "test_A_2"
   class_ptr.date = "test_A_3"
   class_ptr.add_rec()

   class_ptr.id = None
   class_ptr.titre = "test_B_1"
   class_ptr.code = "test_B_2"
   class_ptr.date = "test_B_3"
   class_ptr.add_rec()

   class_ptr.titre = "test_C_1"
   class_ptr.code = "test_C_2"
   class_ptr.date = "test_C_3"
   class_ptr.add_rec()

   class_ptr.titre = "test_A_2"
   class_ptr.code = "test_A_3"
   class_ptr.date = "test_A_4"
   class_ptr.add_rec()

   class_ptr.titre = "test_B_2"
   class_ptr.code = "test_B_3"
   class_ptr.date = "test_B_4"
   class_ptr.add_rec()

   class_ptr.titre = "test_C_2"
   class_ptr.code = "test_C_3"
   class_ptr.date = "test_C_4"
   class_ptr.add_rec()

   class_ptr.list_all_recs()

   class_ptr.lookup_first_field(1)
   class_ptr.lookup_first_field(5)
   print "lookup id not on file"
   class_ptr.lookup_first_field(9)

   lookup_dic = {"dic_field_1":2, 
                    "dic_field_2":"test_B_1"}
   class_ptr.lookup_first_2_fields(lookup_dic)

##   END  test_data()


##======================================================
if __name__ == '__main__':
   try :
      DM=DummyClass()
      test_data( DM )

   except :
      import traceback
      traceback.print_exc()
      raise

Thank you woooee, i'm gonna take a deeper look to the code you have posted...

Now i need, and i want to have, rest, because i worked all the week...

Thank you for your help!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.