資料庫建立

2021-09-26 08:29:20 字數 1178 閱讀 6422

# -*- coding: utf-8 -*-

"""created on tue aug 20 13:14:16 2019

@author: administrator-kf

"""#匯入資料庫操作模組

import sqlite3

#匯入系統操作模組

import os

#將資料庫名字賦予變數

dbpath=

"test.db"

#判斷資料庫是否已經存在,不存在重新建立,否則進行提示

ifnot os.path.exists(dbpath)

:# 連線資料庫,不存在則當前目錄建立

conn = sqlite3.connect(dbpath)

# 建立乙個cursor之後,就可以使用sql語句來對資料進行操作,建立表、新增資料、遍歷資料等。

cur = conn.cursor(

)# 執行語句

#建立表 test 為表名,存在3個屬性

# 欄位名輸入,先說變數名,再說變數型別

cur.execute(

"create table test(name text,age int,gender text)"

)# 關閉游標

cur.close(

)#提交上述操作

conn.commit(

)# 關閉連線

conn.close(

)#列印操作成功,為自己祝賀

print

('建立資料庫%s成功'

% dbpath)

else

:print

("資料庫%s已經存在"

%dbpath)

python自帶sqlite資料庫,無需額外安裝,使用sqlite資料庫需要匯入sqlite3模組

在資料庫建立階段最重要的語句是

「create table 」

其他語句是運算元據庫的普遍語句

開始–>建立connection–>獲取cursor–>執行sql語句,處理資料結果–>關閉cursor–>關閉connection–>結束

sqlite存放資料的型別

null 空值

integer 整型

varchar 可變長度的字元資料

text 文字字串

boolean 布林

資料庫建立

刪除資料庫 drop database cpxs 資料庫操作語法 建立資料庫 create database database name on 子句 資料檔案屬性 primary 指定主檔案,否則預設第乙個 n 檔案屬性 filegrowth growth increament n 檔案組 log ...

建立資料庫

1.建立表空間 create tablespace test space logging datafile f oracle oracledata test.ora size 5m extent management local uniform segment space management au...

建立資料庫

建立乙個資料庫 binbin 有乙個主要資料庫檔案 bin.mdf 乙個次要資料庫檔案組 jay 裡面有次要資料檔案 jay 1.ndf,jay 2.ndf 乙個日誌文志檔案 ziji.ldf 判斷是否存在此資料庫,若存在就刪除 if exists select from sysdtabases w...