資料庫操作 1

2021-10-18 07:39:16 字數 4706 閱讀 8216

import os

import sqlite3

class

******toolsql()

:"""

******toolsql for sqlite3

簡單資料庫工具類

"""def __init__

(self, filename=

"stsql"):

""" 初始化資料庫,預設檔名 stsql.db

filename:檔名

"""self.filename = filename +

".db"

self.db = self.

create_dabase()

# 建立游標

self.cur = self.db.

cursor()

def __del__

(self)

:"""

關閉資料庫

"""self.cur.

close()

self.db.

close()

def create_dabase

(self)

:"""

連線資料庫, 如果資料庫不存在建立資料庫

允許跨執行緒運算元據庫

"""return sqlite3.

connect

(self.filename, check_same_thread=false)

def execute

(self,sql, param=none)

:"""

執行資料庫的增、刪、改

sql:sql語句

param:資料,可以是list或tuple,亦可是none

retutn:成功返回true

"""try

:if param is none:

self.cur.

execute

(sql)

else:if

type

(param) is list:

self.cur.

executemany

(sql, param)

else

: self.cur.

execute

(sql, param)

count = self.db.total_changes

self.db.

commit()

except exception as e:

print

(e)return false,e

if count >0:

return true

else

:return false

def get_all_table_name

(self)

:"""

sqlite_master:為系統表

系統表結構

sqlite_master

( type text, #型別:table-表,index-索引,view-檢視

name text, #名稱:表名,索引名,檢視名

tbl_name text,

rootpage integer,

sql text

)""" self.cur.

execute

(f"select name from sqlite_master where type='table'"

) table_names = self.cur.

fetchall()

return table_names

def query

(self,sql, param=none)

:"""

查詢語句

sql:sql語句

param:引數,可為none

retutn:成功返回true

"""if param is none:

self.cur.

execute

(sql)

else

: self.cur.

execute

(sql,param)

return self.cur.

fetchall()

# def set(self,table,field=" * ",where="",iswhere=false):

# self.table = table

# self.filed = field

# if where != "" :

# self.where = where

# self.iswhere = true

# return true

if __name__ ==

"__main__"

:"""

測試**

"""sql =

******toolsql

("test"

) # 建立表

# f = sql.execute("create table test (id int not null,name text not null,age int);")

sql.

execute

("create table koukou (id integer primary key,name text not null,age int);"

)

# 插入值

# sql.execute("insert into koukou (name,age) values (?,?);",[('abc',15),('bca',16)])

# sql.execute("insert into test (id,name) values (?,?);",(3,'bac'))

#查詢值

# res = sql.query("select * from test;")

# print(res)

# command = "update koukou set name = age"

# sql.execute(command) 123

sqlite 語句

1 修改 name 列 id =

2 位置處 值為 koujw

command =

"update koukou set name = 'koujw' where id = 2"

2 修改表名

command =

"alter table koukou rename to koukou_bank"

3 新增列

command =

"alter table koukou_bank add column *** char(1)"

4 建立 json 資料型別

command =

"create table a(sentenc json)"

5 建立json資料,並插入值

command =

"create table a(sentenc json)"

cmmand =

'insert into a values(json_object("mascot", "our mascot is a dolphin name sakila"));'

刪除

1 刪除指定資料 

delete from `表名` where 條件

2 清空表

truncate `表名`

delete vs truncate

相同:

都能刪除資料,都不會刪除表結構

不同: truncate : 重新設定自增列,計數器歸零

delete : 不影響事務, 計數器不歸零

查詢

1 查詢表所有內容

select * from 表名

2 查詢指定字段

select `列名1`, `列名2` from 表名

3 查詢指定字段,並給字段起別名,也可給表起名

select `列名1` as 名字1, `列名2` as 名字2 from 表名 as 新錶名

4 函式呼叫

select 函式() from 表名

查詢結果拼接字串

select concat(`姓名: `, 列名) as 新列名 from 表名

5 去重,重複的資料只顯示一次

select distinct `列名` from 表名

6 列元素操作,列 加2

select `列名` + 2 from 表名

模糊查詢

like

%(匹配0到任意個字元)

_(匹配乙個字元)

select `列名1` from 表名 where 列名1 like `劉%`

select `列名1` from 表名 where 列名1 like `劉_`

select `列名1` from 表名 where 列名1 like `劉__`

資料庫 1 基礎 資料庫操作

create database if not exists db name charset set charset collate collation create database if not exists db name charset set charset collate collatio...

資料庫基本操作1

1.建立資料庫 create database if not exists db name charset collate 不指定的話預設charset是utf8 預設效驗規則是utf8 general ci 不區分大小寫 utf8 bin區分大小寫 2.檢視資料庫 show database 檢視...

SQLITE 資料庫操作(1)

1.檢視sqlite版本 sqlite3 version2.進入sqlite後台操作 sqlite3 sgbase.db 有時在root目錄下直接輸這個命令無法開啟資料庫,應該加sgbase.db資料庫的路徑,例如 sqlite3 etc config sgbase.db 3.檢視所有資料庫 dat...