python Django串連MySQL資料庫做增刪改查_python

來源:互聯網
上載者:User

1、下載安裝MySQLdb類庫
http://www.djangoproject.com/r/python-mysql/
2、修改settings.py 配置資料屬性

複製代碼 代碼如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'djangodb',                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
        'PORT': '3306',                      # Set to empty string for default.
    }
}

修改完後進入DOS進入項目目錄下執行python manage.py shell命令啟動互動介面輸入一下代碼驗證資料庫配置是否成功。沒報錯則成功!
複製代碼 代碼如下:

>>> from django.db import connection
>>> cursor = connection.cursor()

 3、建立一個Django app
一個項目中包含一個或多個這樣的app。app可以理解為一塊功能集合。比如產品管理模組就包含增刪該查等功能,可以把產品管理叫做一個app。每個Django app都有獨立的models,views等,易移植和被複用。
DOS進入項目目錄 執行 python manage.py startapp products組建目錄檔案如下:
複製代碼 代碼如下:

products/
    __init__.py
    models.py
    tests.py
    views.py

  4、編寫models
複製代碼 代碼如下:

from django.db import models
# Create your models here.
class Company(models.Model):
    full_name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    tel = models.CharField(max_length=15,blank=True)
class Product(models.Model):
    product_name = models.CharField(max_length=30)
    price = models.FloatField()
    stock = models.IntegerField(max_length=5)
    company = models.ForeignKey(Company)

  5、模型安裝(修改settings.py)
複製代碼 代碼如下:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
     'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
     'django.contrib.admindocs',
    'DjangoMysqlSite.products',
)


採用 python manage.py validate 檢查模型的文法和邏輯是否正確。
沒有錯誤則執行 python manage.py syncdb建立資料表。
現在你可以看到你的資料庫除了產生了products_company,products_product外還建立了其它好幾個表,這些是django管理後台所需表暫不管。

6、簡單的增刪改查
 進入python manage.py shell
複製代碼 代碼如下:

from DjangoMysqlSite.products.models import Company
>>> c = Company(full_name='集團',address='杭州西湖',tel=8889989)
>>> c.save()

>>> company_list = Company.objects.all()
>>> company_list

>>> c = Company.objects.get(full_name="集團")
>>> c.tel = 123456
>>> c.save()
 

>>> c = Company.objects.get(full_name="集團")
>>> c.delete()
#刪除所有
>>> Company.objects.all().delete()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.