一、數(shù)據(jù)庫工具的使用
1.下載地址:
https://sqlitemanager.en.softonic.com/
2.安裝說明:
輸入注冊碼:SQLM3-29H3A-23383-LDCZD妨猩,名稱捂襟、公司隨意輸入,出現(xiàn)下圖提示則破解成功
3.sqlitemanager工具使用:
二趴酣、python 創(chuàng)建梨树、增、刪岖寞、改抡四、查
#1.導(dǎo)入SQLite驅(qū)動
import sqlite3
#2.數(shù)據(jù)庫文件格式為.db,不存在仗谆,則自動創(chuàng)建
conn = sqlite3.connect('D:/test.db')
#3.創(chuàng)建一個cursor(游標(biāo))
cursor = conn.cursor()
#4.執(zhí)行sql語句
cursor.execute(sql)
#5.關(guān)閉Cursor
cursor.close()
#6.提交事務(wù)
conn.commit()
#7.關(guān)閉connection:
conn.close()
sql語句:
#創(chuàng)建表
tables_sql='''CREATE TABLE INFO
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);'''
#插入數(shù)據(jù)
insert_sql="INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
VALUES (1, 'Paul', 32, 'California', 20000.00 )"
#變量插入
name='make'
age='18'
insert_sql="INSERT INTO COMPANY (name指巡,age) \
VALUES ('%s','%d')"%(name,age)
#查詢數(shù)據(jù)
select_sql="SELECT * from INFO"
sql_list=cursor.fetchall() #使用featchall獲得結(jié)果集(list)
#修改數(shù)據(jù):
update_sql="UPDATE INFO set NAME="Make" where type="Paull""
#刪除數(shù)據(jù):
delete_sql="DELETE from INFO where ID=2"
三、簡單封裝使用
import sqlite3
class dbutils():
conn=''
cursor=''
def __init__(self):
pass
#鏈接庫/創(chuàng)建庫
def create(self,file):
self.conn=sqlite3.connect(file)
self.cursor = self.conn.cursor()
#執(zhí)行sql
def execute(self,sql):
self.cursor.execute(sql)
self.conn.commit()
#關(guān)閉
def close(self):
#先關(guān)閉游標(biāo)再關(guān)閉數(shù)據(jù)庫鏈接
self.cursor.close()
self.conn.close()
if __name__ == '__main__':
db=dbutils()
db.create('D:/mysql.db')
sql="CREATE TABLE op (type CHAR(20),Remarks CHAR(20),len INT,rows INT)"
db.execute(sql)
db.close()