In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Database engine
Pymysql takes its place-recommendation
If 2, pip install pymysql
If 3, pip3 install pymysql
After the installation is complete, use pip3 freeze to check what version is installed and so on.
MySQLdb python3 does not support it, and TM has not been updated for a long time.
Sudo yum install python-devel mysql-devel
MySQL-python-1.2.5, this is the mysql interface of python2.7.6.
Https://pypi.python.org/pypi/MySQL-python/1.2.5
Import MySQLdb
Mysqlclient django1.9 recommendation, python3.3+ support
Sudo yum install python-devel mysql-devel
Pip install mysqlclient
MySQL Connector/Python
$python manage.py makemigrations polls stored as amigration.$ python manage.py sqlmigrate polls 0001 just returns their SQL$ python manage.py check # this checks for any problems in your project without making migrations or touching the database.$ python manage.py migrate apply those changes to the database. The primary key _ id is automatically generated
Python manage.py shell
Basic data access > from books.models import Publisher > p1 = Publisher (name='Apress', address='....) > p1.save () > publisher_list = Publisher.objects.all () > publisher_list []
``objects.create () ``does not require save if the object is created and stored in the database in one step
> p1 = Publisher.objects.create (name='Apress',... Address='2855 Telegraph Avenue',.
Add the string representation of the module
Add a method _ _ unicode__ () to the Publisher object.
From django.db import modelsclass Publisher (models.Model): name = models.CharField (max_length=30) def _ unicode__ (self): return self.name > from books.models import Publisher > publisher_list = Publisher.objects.all () > publisher_list [,]
Data filtering
Filter () returns the collection
You can pass multiple parameters to filter () to narrow the selection:
> > Publisher.objects.filter (country= "U.S.A.", state_province= "CA") []
There is a double underscore between name and contains. The contains part is translated into LIKE statements by Django:
> Publisher.objects.filter (name__contains= "press") [] Note that the default = operator of SQL matches exactly
SELECT id, name, address, city, state_province, country, websiteFROM books_publisherWHERE name LIKE'% press%'; other lookup types are: icontains (case-independent LIKE), startswith and endswith, and range (SQLBETWEEN query). Appendix C describes all the lookup types in detail.
Get a single object. ``get () ``returns a single object
> Publisher.objects.get (name= "Apress") if the result is multiple objects, an exception will be thrown, and an exception will be thrown if the query does not return a result:
DoesNotExist: Publisher matching query does not exist.Publisher.DoesNotExist in your application, you can catch and handle this exception, like this:
Try: P = Publisher.objects.get (name='Apress') except Publisher.DoesNotExist: print "Apress isn't in the database yet." else: print "Apress is in the database." Data sorting
Orderly return of query results
If you need to sort by multiple fields (the second field is used if the value of the first field is the same), the minus sign-
Indicates reverse sorting:
> > Publisher.objects.order_by ("- state_province", "address") [,]
Limit the data returned
Take out a fixed number of records, cut sentences, do not support negative index, but can be queried in reverse order
> Publisher.objects.order_by ('- name') [0:2]
Update multiple objects
Update a record
> Publisher.objects.filter (id=52) .update (name='Apress Publishing')
Update multiple records.
> Publisher.objects.all () .update (country='USA') # the country field 2 # of all Publisher indicates the number of entries affected: the save () method, which updates all columns in a row. In some cases, we only need to update a few columns in the row.
Delete an object Delete an object in the database simply by calling the object's delete () method:
> p = Publisher.objects.get (name= "O'Reilly") > > p.delete () > > Publisher.objects.all () [] > Publisher.objects.filter (country='USA'). Delete () # Delete part > Publisher.objects.all (). Delete () # Delete all data in a table to prevent accidental deletion. Django requires that all data in the table be displayed with all (). Otherwise, report an error.
> Publisher.objects.delete () # must add all () Traceback (most recent call last): File "", line 1, in AttributeError: 'Manager' object has no attribute' delete'
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.