Python sqlite3常用語句

2021-08-21 10:40:32 字數 1795 閱讀 7168

日拱一卒無有盡,功不唐捐終入海。

常見的儲存資料的三種方式

1.記憶體儲存:變數      優點:讀寫速度快      缺點:程式關閉,記憶體釋放

2.檔案儲存:檔案讀寫操作      優點:資料永久    缺點:讀寫操作麻煩

資料庫即為資料儲存倉庫

3.資料庫儲存:  優點:資料永久      缺點:學習難度大

資料庫按性質劃分有兩種:

1.關係型資料庫:資料與資料之間有著緊密的聯絡

優點:可以進行多表查詢

缺點:刪除維護資料麻煩

牽一發動全身       mysql       splite

2.非關係型資料庫:資料和資料之間沒有聯絡

優點:對資料進行增刪維護簡單   缺點:資料之間少了耦合性

一人吃飽    全家不餓       mongodb       redis

資料庫按照使用規模來劃分

可以分為四個等級

1.大型資料庫    一般用於大型商業公司         例如**,京東       代表  oracle

2.中型資料庫     使用非常廣泛的資料庫      代表 sqlserver

3.小型資料庫     一般用於曉得產品公司或者公司內部資料庫      代表 mysql

4.微型資料庫       經常用於移動端                    代表sqlite

import  aplite3

建立乙個資料庫

con =splite3.connect('mydb(2)')

建立乙個資料庫游標

使用游標對資料表進行增刪改查等工作

cursor =con.cursor()

使用游標命令:建立一張表  如果不存在的話

cursor.exexute('create  table  if  not  exists  mytable(name text,age int )')

com.commit()

insert.into插入資料到指定的表中

cursor.execute('insert  into  mytable(name,age)values("city","19")')

con.commit()

刪除資料

cursor.execute('delete  from  mytable   where   name="jack"')

con.commit()

修改資料

cursor.execute('update  mytable  set  name="******",age=33  where name ="city"')

con.commit()

查詢資料

cursor.execute('select*from   mytable')

cursor.execute('select *  from  mytable  where   name="city"')

cursor.execute('select*from  mytable  where   age>0')

fetch   抓取   得到

result =cursor.fetchall()

result =cursor.fetchone()

many()裡面的數字表示獲取幾條資料

result =cursor.fetchmany(3)

print(result)

慎用   刪除整個表

cursor.execute('drop  table  if  exists  mytable')

con.commit()

python sqlite3學習筆記

self.connect sqlite3.connect db name,timeout 3,isolation level none,check same thread false 引數1 db name 資料庫名稱 引數2 timeout 3 指當乙個資料庫被多個連線訪問,且其中乙個修改了資料庫...

python sqlite3查詢表記錄

desc 指 降序 解決的方法是 按照id 逆序排列,選取前10個 select from log info order by id desc limit 10 asc 指公升序 解決的方法是 按照id公升序排列,選取前10個 select from log info order by id asc...

Python Sqlite3以字典形式返回查詢結果

sqlite3本身並沒有像pymysql一樣原生提供字典形式的游標。cursor conn.cursor pymysql.cursors.dictcursor 但官方文件裡已經有預留了相應的實現方案。def dict factory cursor,row d for idx,col in enume...