1

我们正在开发基于文本的游戏 (MUD),并且已经遇到了这个障碍。

编码:

class RoomMove():

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

[额外代码无关紧要]

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()

它会抛出此错误:

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

编辑:我已经尝试过 (room, ) .. 不过错误没有区别。

有任何想法吗?

谢谢!

4

2 回答 2

1

正如您在文档中所读到的:

获取一个()

获取查询结果集的下一行,返回单个序列,或者当没有更多数据可用时返回 None。

如果没有更多数据 c​​.fetchone() 将 None 分配给 spaceCheck 变量,因此当您尝试访问 spaceCheck[counter] 时会出现 TypeError。

尝试这样的事情:

        if spaceCheck is not None:
            counter += 1

更新

你应该更换

room = (room)

和:

room = (room, )

另一个更新

假设您创建如下表:

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

您的代码可能与此类似:

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()
于 2013-09-07T21:39:19.597 回答
0

什么解决了上面显示的问题:

sqlite3 不读取整数,只读取字符串。(实际上并非如此)

所以我在 else 循环中将整数作为字符串读取。

这里:

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

房间被作为整数传递给 c.execute,解决方案?

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

花了很长时间才看到!

于 2013-09-07T22:48:59.190 回答