[問題] 請問Django如何新增column

看板Python作者 (Jason1122)時間12年前 (2011/09/07 15:11), 編輯推噓4(407)
留言11則, 4人參與, 最新討論串1/1
我是用sqlite3資料庫,python2.7 跟著Django官方網站教學在做 要加入一個column: num_pages = models.IntegerField(blank=True, null=True) 書上說要做幾個動作 1. Add the field to your model. 2. Run manage.py sqlall [yourapp] to see the new CREATE TABLE statement for the model. Note the column definition for the new field. 3. Start your database’s interactive shell (e.g., psql or mysql, or you can use manage.py dbshell). Execute an ALTER TABLE statement that adds your new column. 我前兩個完成了,第三個說Start your database’s interactive shell sqlite3本身就跟python2.7在一起,我是直接將ALTER TABLE打在python shell裡 ALTER TABLE books_book ADD COLUMN num_pages integer 顯示SyntaxError 想說在cmd中執行manage.py dbshell看看,也顯示sqlite3錯誤訊息 不知該怎麼增加新的欄位了 還請幫忙解決,謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.32.115.169

09/07 15:23, , 1F
manage.py dbshell 的錯誤訊息是?
09/07 15:23, 1F

09/07 16:23, , 2F
$ sqlite3 dbname 這可以嗎
09/07 16:23, 2F

09/07 16:36, , 3F
我都用 firefox + addons "SQLite manager"
09/07 16:36, 3F

09/07 17:02, , 4F
settings.py裡面DATASASES要設django.db.backends.sqlite3
09/07 17:02, 4F

09/07 17:02, , 5F
而不是只打sqlite3
09/07 17:02, 5F

09/07 17:03, , 6F
以上純粹猜測...
09/07 17:03, 6F
我本來是只打sqlite3而已,剛剛改成django.db.backends.sq,輸入manage.py dbshell 後還是不行...,同樣的錯誤訊息,(sqlite3,不是內部或外部命令,可執行的程式或 批次檔。) db我是像下面這樣設定 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'C:/Python27/Lib/site-packages/django/bin/mysite/db/test.db', 'USER': '', # Not used with sqlite3. 'PASSWORD': '', # Not used with sqlite3. 'HOST': '', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '', # Set to empty string for default. Not used with sqlite3. } } ※ 編輯: Jason1122 來自: 114.32.115.169 (09/07 19:11)

09/07 19:42, , 7F
要加一個column其實不用這麼麻煩, python manage.py syncdb
09/07 19:42, 7F

09/07 19:43, , 8F
就會自己新建一個了
09/07 19:43, 8F

09/07 19:43, , 9F
不過錯誤訊息看起來感覺是你的python path沒有設定好
09/07 19:43, 9F
剛剛找到類似的問題,http://www.zeuux.org/group/django/bbs/content/6442/ 照著做解決了,要再去下載db shell,設定好目錄位置之後再執行python manage.py 就可以進入了 ※ 編輯: Jason1122 來自: 114.32.115.169 (09/07 20:06)

09/07 20:26, , 10F
good job!
09/07 20:26, 10F

09/08 00:55, , 11F
Firefox的addons也很好用
09/08 00:55, 11F
文章代碼(AID): #1EPnaHf2 (Python)