In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
How to get started with web.py database? in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Web.py is a very delicate web framework, but its own db module is also very concise and efficient. Compared with the complex JDBC and tedious Hibernat configuration in Java, using web.db is simple and straightforward.
Create a database object:
Db= web.database (dbn='mysql', user='user', pw='pass', db='dbname')
Dbn specifies the database type
Query:
Users = db.query ('select * from user where id > $id', vars= {' id':100})
For user in users: print user.id, user.name
Query parameters are represented by $var_name and replaced by values in vars dict when querying
The result of the query is an iterative object and a direct loop.
As for the specific properties of each object, it corresponds to the field name one by one. There is no predefined class, no mapping and configuration, everything is a convention, what you need is to manage the naming of database fields yourself.
Insert:
Db.insert ('user', name='Michael', age=29, passwd='passwd', email='abc@xyz.com')
It is very convenient to insert the field value provided by the * * kw of python.
Modify:
Db.update ('user', where='id=$id', vars= {' id':100}, name='Michael', age=29)
Update also makes full use of the * * kw parameter of python. Only the * * kw passed in is update, and the other fields remain unchanged.
Where and vars are responsible for generating where statements and binding parameters.
Delete:
Db.delete ('user', where='id=$id', vars= {' id':100})
Similar to update, except that there is no * * kw because delete only needs the where clause.
Compared with Java, the db operation of web.py is very simple, mainly due to the * * kw parameter of python and the built-in dict support (corresponding to the Map of Java)
Just imagine, if you use Java's Map to pass in parameters, you have to write:
Map where_vars = new HashMap ()
Where_vars.put (id, 100)
Map update_vars = new HashMap ()
Update_vars.put ("name", "Michael")
Update_vars.put ("age", 29)
Db.update ("user", "where id=$id", where_vars, update_vars)
No wonder I'm bored to death.
When using Java for web development, we are always used to dividing it into web,logic,dao tiers. However, when we can do all the work with a small amount of code, we really don't see the meaning of layering. I use web.py to write all the logic of the site in only 600 lines of code, so there is only one app.py file for the whole site, and there is no need to split web and db at all.
Dynamic language is a trend, especially in web applications.
This is the answer to the question about how to start the web.py database. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.