ラベル mysql の投稿を表示しています。 すべての投稿を表示
ラベル mysql の投稿を表示しています。 すべての投稿を表示

2017年11月30日木曜日

DjangoのModel使用

1)Modelを定義
appフォルダーにmodels.pyにTableとColumnを定義
from django.db import models

# Create your models here.
class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

2)appをProjectに結び付く
settings.pyのINSTALLED_APPSにappを追加

INSTALLED_APPS = [
    'polls.apps.PollsConfig',
    .....

3)migrations作成
python manage.py makemigrations polls

4)migrate実施
python manage.py sqlmigrate polls 0001  #どんなSQL実行されるか確認
python manage.py migrate

Django appを作成

1)Projectを作成、cloud9環境ではすでに作成されたので、不要
django-admin startproject mysite

2)appを作成
python manage.py startapp polls

Projects vs. apps
What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

3)Viewを作成
polls/views.py を編集

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

4)To call the view, we need to map it to a URL
 polls/urls.py を作成

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

5)To point the root URLconf at the polls.urls module

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

6)DBの設定
MySQLのアクセスモジュールを追加
sudo pip3 install PyMySQL

Projectフォルダーにsettings.pyのDATABASESを変更
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'c9',  #dbname
        'USER': 'XXXXc9',       #username
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
    }
}

manage.pyに以下を追加
import pymysql
pymysql.install_as_MySQLdb()

7)migrate実施
python manage.py migrate
settings.pyのINSTALLED_APPSに必要なTableが作成されれば、DB設定OK


2017年5月25日木曜日

laravel5.4 DB migration

1)table作成
php artisan make:migration create_posts_table
php artisan make:migration create_categories_table
php artisan make:migration create_comments_table

database/migrationsに新たに3つファイルが作成された、それぞれ以下のように編集

2017_05_23_143039_create_posts_table
public function up()
 {
  Schema::create('posts', function($table){
   $table->increments('id');
   $table->string('title');
   $table->string('cat_id'); // ポストテーブルとカテゴリーテーブルの紐付けに利用します
   $table->text('content');
   $table->unsignedInteger('comment_count'); // 投稿に何件のコメントがついたのかをカウントします
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('posts');
 }

2017_05_23_143100_create_categories_table
public function up()
 {
  Schema::create('categories', function($table){
   $table->increments('id');
   $table->string('name');
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('categories');
 }
2017_05_23_143113_create_comments_table
public function up()
 {
  Schema::create('comments', function($table){
   $table->increments('id');
   $table->unsignedInteger('post_id'); // ポストテーブルとコメントテーブルの紐付けに利用します
   $table->string('commenter');
   $table->text('comment');
   $table->timestamps();
  });
 }

 /**
  * Reverse the migrations.
  *
  * @return void
  */
 public function down()
 {
  Schema::drop('comments');
 }
php artisan migrate でtableが作成される

下記エラーになる場合、対応が必要。(mysql5.7.7以降のバージョンなら対応不要)
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

app/Providers/AppServiceProvider.php に以下のように追加

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}


参考:https://manablog.org/laravel_bulletin_board/
https://laravel-news.com/laravel-5-4-key-too-long-error

2016年10月14日金曜日

cloud9でmysql/phpmyadminの使用

シェルで
mysql-ctl start
phpmyadmin-ctl install

https://{app_address}/phpmyadmin  にアクセス

2016年6月8日水曜日

mysqlをMacにインストール

1)dmgファイルをインストール
2)システム環境設定に「MySQL」が出来上がる、そこからmysqlを起動できる
3)初期passwordを変更
mysqladmin password NEWPW -u root -p
初期passwordを入力したら、NEWPWに変更される