4,修改: cu.execute("update catalog set name='name2' where id = 0")
cx.commit() 注意,修改数据以后提交
5,删除: cu.execute("delete from catalog where id = 1") cx.commit() 以上简单的操作反应的Python SQLITE数据库操作的基本要点,这里点到为止.然后,SQLite的强大,并不仅限于此,其对SQL高级特性的支持及其小巧灵活的特点,使得SQLite在众多领域受到开发者的青睐.
思路与实践
目前想不出啥高级用法于是只简单建立数据库并进行查询。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
definit_database(path):#初始化数据库 conn = sqlite3.connect(path) c = conn.cursor() sql = ''' create table music( name text, sname text, link text, sID text ) ''' c.execute(sql) conn.commit() conn.close()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
defSavedata2(path):#将排行榜数据存入数据库里 init_database(path) conn = sqlite3.connect(path) cur = conn.cursor() for i inrange(len(Sing)): item = Sing[i] index = Singer[i] dd = ''' insert into music( name,link,sname,sID ) values("%s","%s","%s","%s") ''' % (item[0],item[1],index[0],index[1]) cur.execute(dd) conn.commit() conn.close() print("OVER")
from bs4 import BeautifulSoup import re import sqlite3 #进行SQlite数据库操作 from selenium import webdriver from selenium.webdriver.chrome.service import Service
defAskurl(url): s = Service(r"E:\python\PyCharm Community Edition 2021.3.1\plugins\python-ce\helpers\typeshed\stubs\selenium\selenium\webdriver\chrome\chromedriver.exe") driver = webdriver.Chrome(service=s) driver.get(url) driver.switch_to.frame("g_iframe") text = driver.page_source return text
html = Askurl(url) soup = BeautifulSoup(html,"html.parser")
for item in soup.find_all('div', class_="ttc"): item = str(item) data = [] name = re.findall(FindName, item)[0] name = name.replace('\xa0'," ") data.append(name)