Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self.tbl在mysql.py中没有定义 #1

Open
zhengjinzhj opened this issue Dec 12, 2016 · 4 comments
Open

self.tbl在mysql.py中没有定义 #1

zhengjinzhj opened this issue Dec 12, 2016 · 4 comments

Comments

@zhengjinzhj
Copy link

没看到你在mysql.py这个文件中定义self.tbl就直接在48行使用了。
另外,Model类中的select方法,没有必要对例如select_str = '123, abc' 中的逗号进行处理吧,可以直接用select 123, abc from tbl。为什么非要用select '123', 'abc' from tbl呢?
请大神指点一二,谢谢!

@octans
Copy link
Owner

octans commented Dec 12, 2016

  1. 在python的类中,你可以直接对成员变量赋值,比如:
class User:
     def set_name(self, name):
         self.name = name
     def get_name(self):
         return self.name

 u = User()
 u.set_name('tiger')
 print(u.get_name())

运行后将打印出'tiger'

➜  PythonPractice git:(master) ✗ python3 class.py
tiger
  1. select语句中给字段加的是反单引号,目的是防止有的字段名称和mysql的关键字重名,比如order, desc等关键字,比如我有如下表:
mysql> desc user;
+-------+------------------+------+-----+---------+-------+
| Field | Type             | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id    | int(10) unsigned | NO   | PRI | 0       |       |
| name  | varchar(100)     | YES  |     | NULL    |       |
| order | int(10) unsigned | YES  |     | NULL    |       |
+-------+------------------+------+-----+---------+-------+

执行select order from user 就会报错,执行select order from user 就OK

mysql> select order from user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order from user' at line 1
mysql> select `order` from user;
Empty set (0.00 sec)

当然表user在创建时,字段order也被返单引号括起来的。
总之用反单引号将mysql字段名括起来是好的编程习惯

@zhengjinzhj
Copy link
Author

第二个问题明白了,感谢解惑!
还是第一个问题,我知道可以直接在类中对成员变量赋值,但是这个类中没有tbl这个成员变量啊。请问,你这个Model类是继承的哪个类?

class Model:
    sql = ''
    def select(self, select_str):
        if select_str.find(",") == -1:  # test = '123abc', test.find(',') = -1, test.find('a') = 3
            select_str = select_str
        else:
            fields = list()
            for f in select_str.split(","):
                fields.append("'" + f.strip() + "'")
                select_str = ",".join(fields)
        self.sql = "SELECT " + select_str + " FROM " + self.tbl
        return self

    def where(self, string):
        self.sql = self.sql + " WHERE " + string
        return self

    def order_by(self, string):
        self.sql = self.sql + " ORDER BY " + string
        return self

    def limit(self, num):
        self.sql = self.sql + " LIMIT " + str(num)
        return self

    def fetch_all(self):
        return self.conn.query(self.sql).cursor.fetchall()

我这里self.tbl和self.conn都会提醒Unresolved attribute reference 'tbl'('conn') for class 'Model'
我用的是Python 2.7.12,难道是这个问题?

@octans
Copy link
Owner

octans commented Dec 13, 2016

哦哦,class Model,我是这样使用的,所以没有发现你说的报错。但直接调用Model的话,确实会报错的,后续我修复一下你说的这个bug,感谢

### 文件wanghong.py line353
class BoseModel(Model):
    conn = Mysql(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd='123456', db='wanghong', charset='utf8')

class WMYXActor(BoseModel):
    tbl = "Tbl_WMYX_Actor"

WMYXActor().insert()

@dirtyluke
Copy link

huajiao.py中使用了Model类,但是我这却导不进来,我用的Python3.6,官方自带的mysql模块

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants