django使用MySQL資料庫流程

來源:互聯網
上載者:User

標籤:

1.資料移轉
不同的app串連不同的資料庫(例子中blog用預設的資料庫,blog4用MySQL資料庫)

(1)要串連MySQL資料庫,首先下載MySQL資料庫和MySQLdb模組

下載MySQL資料庫的參考串連:

http://www.cnblogs.com/onlycxue/p/3291889.html
而MySQLdb模組安裝參考串連:https://pypi.python.org/pypi/MySQL-

python/1.2.4 (我是下載的.exe)


(2)接下來就是建立django的工程什麼的就不詳細講了(我的工程裡有blog和blog4兩個app

)


(3)修改setting.py設定檔
DATABASES = {
‘default‘: {
‘ENGINE‘: ‘django.db.backends.sqlite3‘,
‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘),
},
‘db2‘: {
‘ENGINE‘:‘django.db.backends.mysql‘,
‘NAME‘:‘dbname2‘,
‘USER‘:‘root‘,
‘PASSWORD‘:‘5302613‘,
‘HOST‘:"127.0.0.1",
},
}

DATABASE_ROUTERS = [‘mysite.database_router.DatabaseAppsRouter‘]
DATABASE_APPS_MAPPING = {
‘blog4‘: ‘db2‘,
}

(4)blog4的models.py檔案的代碼都是如下:

from __future__ import unicode_literals
from django.db import models

# Create your models here.
class Blog4(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()

def __unicode__(self):
return self.title

(5)在工程的檔案夾中的同名檔案夾mysite.py中添加一個database_router.py檔案
代碼在這個網站複製:http://www.ziqiangxuetang.com/django/django-multi-

database.html

(7)進入到工程的目錄,輸入一下命令:
>>>python manage.py makemigrations

Migrations for ‘blog4‘:
blog4\migrations\0001_initial.py:
- Create model Blog4


>>>mysql -u root -p; (進入MySQL資料庫)

>>>create database dbname2;

mysql> create database dbname2;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbname1 |
| dbname2 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)

可以看到我建立的dbname2資料庫成功了


>>>python manage.py migrate --database=db2 (為blog3同步到MySQL資料庫)

Operations to perform:
Apply all migrations: admin, auth, blog, blog2, blog3, blog4, contenttypes, se
ssions
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying blog.0001_initial... OK
Applying blog2.0001_initial... OK
Applying blog3.0001_initial... OK
Applying blog4.0001_initial... OK
Applying blog4.0002_auto_20160323_1429... OK
Applying sessions.0001_initial... OK

看到上面,證明同步到MySQL資料庫成功了!!!


進入到MySQL資料庫看看是否成功:

mysql> use dbname2;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_dbname2 |
+----------------------------+
| auth_group |
| auth_group_permissions |
| auth_permission |
| auth_user |
| auth_user_groups |
| auth_user_user_permissions |
| blog2_blog2 |
| blog3_blog3 |
| blog4_blog4 |
| blog_blog |
| django_admin_log |
| django_content_type |
| django_migrations |
| django_session |
+----------------------------+
14 rows in set (0.00 sec)

這次同步資料庫建立的表是blog4_blog4,可以看到是成功的


(8)操作一下資料庫看看能否插入資料
>python manage.py shell
>>> from blog4.models import Blog4
>>> Blog4.objects.create(title="title 1",content="content 1")
<Blog4: title 1>

在去到資料庫下看看:
mysql> SELECT * FROM blog4_blog4;
+----+---------+-----------+
| id | title | content |
+----+---------+-----------+
| 1 | title 1 | content 1 |
+----+---------+-----------+
1 row in set (0.00 sec)

呵呵,插入成功,搞定收工.....

有些步驟省略了,將就著看

 

參考串連:http://www.cnblogs.com/fengri/articles/django5.html

django使用MySQL資料庫流程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.