首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基本上,我正在尝试编写一个python-sql连接器程序,但是现在我遇到了这个错误

基本上,我正在尝试编写一个python-sql连接器程序,但是现在我遇到了这个错误
EN

Stack Overflow用户
提问于 2019-11-20 05:22:39
回答 1查看 37关注 0票数 0

我得到了错误-->

代码语言:javascript
复制
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

对于这个python -->

代码语言:javascript
复制
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])))

用于将值输入到此mysql表中存在的列中-->

代码语言:javascript
复制
| 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    |       |
+----------+-------------+------+-----+---------+-------+

如果我将一些代码改为str,我会得到下面的结果,我不希望title和quantity列的信息显示为none,我希望它们显示存在于mysql表中的某个值。

代码语言:javascript
复制
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 

如果您能帮助我,我们将不胜感激,谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-11-20 05:41:39

显然,你的一些书有title = NULL,有些销售有quantity = NULL。在尝试格式化它们之前,您需要检查它们。

代码语言:javascript
复制
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])))

顺便说一句,由于您使用的是字符串格式,您可以简单地在数字数据的格式字符串中使用%d,而不是在参数列表中调用str()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58943000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档