主鍵: 是唯一的,通過主鍵可以準确的找到對應的資料,主鍵預設非空,為空時自動增長
準備sqlite語句:
db=sqlite3.connect('xxxxx.db')
建立表格檔案: cerate table 表名(字段1名稱 字段1類型,字段2名稱 字段2類型),例:
sql='cerate table xxxx(id integer primary key,name text,age integer )'
字段類型: integer 無符号整數類型;text 文本字元串;float 浮點型;boolen 布爾類型
添加資訊: insert into 表名(字段名1,字段名2)values(值1,值2),例:
sql="insert into xxxx(id,name,age)values(1,'zz',20)"
修改資料資訊: update 表名 set 修改字段名='修改字段值' where 範圍,例:
sql="update xxxx set name='aa' where id=1"
查詢資料資訊: select*from 表名 where 條件, 例:
sql='select*from xxxx where id=2'
删除資料資訊: delete from 表名 where 删除條件, 例:
sql="delete from xxxx where id=2"
擷取資料遊标:
curse=db.cursor()
如果對資料庫中的表進行了更改,需要送出這次更改,送出以上的操作,執行送出代碼
送出操作:
db.commit()
關閉遊标:
cursor.close()
關閉資料庫:
db.close()
轉載:
Django資料庫基礎操作