简体   繁体   English

Python sqlite3 NoneType错误

[英]Python sqlite3 NoneType error

We're working on a text-based game (MUD) and have hit this roadblock. 我们正在开发基于文本的游戏(MUD),并且遇到了这一障碍。

The code: 编码:

class RoomMove():

    def __init__(self):
        self.room = 1
        self.name = 'Lorinth'
        self.moveRooms()
        self.updateRoom()

[extra code doesn't matter] [多余的代码没关系]

def updateRoom(self):
        global c
        room = str(self.room)
        room = (room)
        print room
        while room > 0:
            c.execute("""SELECT * FROM RoomPlayers where ID=?""", room)
            spaceCheck = c.fetchone()
            print spaceCheck
            counter = 1
            if spaceCheck[counter] not in ('', None):
                counter += 1
            else:
                room = self.room
                name = self.name
                c.execute("""UPDATE RoomPlayers SET '1'=? WHERE ID=?""", (name, room))
        conn.commit()

it throws back this error: 它抛出此错误:

     c.execute("""SELECT * FROM RoomPlayers where ID=?""", room)
ValueError: parameters are of unsupported type

EDITS: I've tried it with (room, ) ..no difference in the error though. 编辑:我已经尝试过(room,)..虽然没有错误的区别。

Any ideas? 有任何想法吗?

Thanks! 谢谢!

As you can read in the documentation : 如您在文档中所读:

fetchone() fetchone()

Fetches the next row of a query result set, returning a single sequence, or None when no more data is available. 获取查询结果集的下一行,返回单个序列,如果没有更多数据可用,则返回None。

If there is no more data c.fetchone() assigns None to the spaceCheck variable, hence TypeError when you try to access spaceCheck[counter]. 如果没有更多数据,则c.fetchone()会将None分配给spaceCheck变量,因此当您尝试访问spaceCheck [counter]时会出现TypeError。

Try something like that: 尝试这样的事情:

        if spaceCheck is not None:
            counter += 1

UPDATE UPDATE

You should replace 你应该更换

room = (room)

with: 有:

room = (room, )

Another update 另一个更新

Assuming you create table like below: 假设您创建如下表:

sqlite> CREATE TABLE RoomPlayers (ID numeric, player_name VARCHAR);

Your code could like similar to this: 您的代码可能类似于以下内容:

class RoomMove():

    def __init__(self, room, name):
        self.room = room
        self.name = name
        self.con = None
        self.db_path = "./foo.sqlite"

    def updateRoom(self):
        try:
            # Globals are evil. Don't use them 
            self.con = sqlite3.connect(self.db_path)
            c = con.cursor()
            # comparing tuple or string to number dosen't make sense. Compare original number
            # You should check if room is valid inside constructor
            #  while room > 0 would probably lead to indefinite loop
            if rooom > 0:
                #You don't have to cast room number to string
                c.execute("""SELECT id FROM RoomPlayers where ID=?""", (self.room, ))
                spaceCheck = c.fetchone()

                # Your counter takes only two values so you can do it like that
                counter = 2 if spaceCheck is not None else 1

                if counter == 1:
                    # Don't name columns '1' - it is really bad idea
                    # Like above - coneverting room and name to string is pointlees
                    c.execute("""UPDATE RoomPlayers SET 'player_name'=? WHERE ID=?""", (self.name, self.room))
                    self.con.commit()

        finally:
            # No matter what happens close connection
            if self.con is not None:
                self.con.close()

What fixed the problem shown above: 解决了上面显示的问题的原因:

sqlite3 doesn't read integers, just strings. sqlite3不读取整数,仅读取字符串。 (not actually true) (实际上不是真的)

So I made the integer be read as a string in the else loop. 因此,我在else循环中将整数读取为字符串。

here: 这里:

else:
        room = self.room
        name = self.name
        c.execute("""UPDATE RoomPlayers SET '1'=? WHERE ID=?""", (name, room))

room is being passed to c.execute as an integer, the solution? 室被传递给c.execute作为整数,解决方案?

else:
    room = str(self.room)
....

Took forever to see that! 永远看到了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM