0

i get the error-->

Traceback (most recent call last):
  File "C:\Users\REJI\Desktop\project\the project\book_store.py", line 346, in <module>
    sale()
  File "C:\Users\REJI\Desktop\project\the project\book_store.py", line 321, in sale
    read_sale()
  File "C:\Users\REJI\Desktop\project\the project\book_store.py", line 217, in read_sale
    print(body%(str(row[0]),row[4][0:20],str(row[2])[0:15],str(row[1]),str(row[3])))
TypeError: 'NoneType' object is not subscriptable

for this python -->

def read_sale():
    qs='''select s1.*,book_title from sale s1, bookmaster b1
where s1.book_no=b1.book_no order by s1.book_no'''
    cur.execute(qs)
    data=cur.fetchall()
    head='''\
               All Sales
+-------+--------------------+------------+----------+----------+
|Book No|       Title        |Date of sale| Quantity |   Price  |
+-------+--------------------+------------+----------+----------+'''
    body='''\
|%7s|%20s|%12s|%10s|%10s|
+-------+--------------------+------------+----------+----------+'''
    print(head)
    for row in data:
        print(body%(str(row[0]),str(row[4])[0:20],str(row[2])[0:15],str(row[1]),str(row[3])))

and which is made for entering values into the columns present in this mysql table-->

| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| book_no  | int(30)     | YES  |     | NULL    |       |
| quantity | varchar(15) | YES  |     | NULL    |       |
| sdate    | date        | YES  |     | NULL    |       |
| price    | int(40)     | YES  |     | NULL    |       |
| title    | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

and if i change some of the code to str ,i get the result below ,i don't want the info for the columns title and quantity to show none i want them to show a certain value that exists in mysql table.

Enter your choice 1-51
               All Sales
    +-------+--------------------+------------+----------+----------+
    |Book No|       Title        |Date of sale| Quantity |   Price  |
    +-------+--------------------+------------+----------+----------+
    |    147|          **None**  |  2019-11-20|  **None**|        50|
    +-------+--------------------+------------+----------+----------+
    sale Menu`enter code here`
            -----------------
            1.To read_sale
            2.To add_sale
            3.TO Exit and return to main menu 

so if you can pls help me, your help will be greatly appreciated thank you

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    The line in the error messsage is not the same as the script. The error message doesn't have `str()` around `row[4]`. It looks like you posted the version of the script that produces the output without the error. – Barmar Nov 19 '19 at 21:36
  • The problem is that the book has `title = NULL`, and the sale has `quantity = NULL`. – Barmar Nov 19 '19 at 21:38
  • According to the TypeError, it looks like the value of row[4] (title) is None. https://stackoverflow.com/a/9320883/5249058 . This could be due to your sql join. Why don't you print each row first just for debugging. – Jzou Nov 19 '19 at 21:58

1 Answers1

0

Apparently some of your books have title = NULL, and some of the sales have quantity = NULL. You need to check for these before trying to format them.

for row in data:
    if row[4] is None:
        row[4] = 'Untitled'
    if row[2] is None:
        row[2] = 0
    print(body%(str(row[0]),row[4][0:20],str(row[2])[0:15],str(row[1]),str(row[3])))

BTW, since you're using string formatting, you could simply use %d in the format string for the numeric data rather than calling str() in the argument list.

Barmar
  • 741,623
  • 53
  • 500
  • 612