<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.5">Jekyll</generator><link href="https://kim-qian.github.io//feed.xml" rel="self" type="application/atom+xml" /><link href="https://kim-qian.github.io//" rel="alternate" type="text/html" /><updated>2024-06-02T09:23:53+08:00</updated><id>https://kim-qian.github.io//feed.xml</id><title type="html">Kim Qian</title><subtitle>Kim-Qian's Webside</subtitle><author><name>Kim Qian</name></author><entry><title type="html">简介</title><link href="https://kim-qian.github.io//2023/08/10/first-post/" rel="alternate" type="text/html" title="简介" /><published>2023-08-10T00:00:00+08:00</published><updated>2023-08-10T00:00:00+08:00</updated><id>https://kim-qian.github.io//2023/08/10/first-post</id><content type="html" xml:base="https://kim-qian.github.io//2023/08/10/first-post/"><![CDATA[<p>这是该网站的第一篇文章，很高兴地与大家分享，以后会在这个网站上分享一些自己写的一些开源的软件(PC/Mobile)和一些自己的生活经历及一些有意思的项目。</p>

<h1 id="网站各板块简介">网站各板块简介</h1>

<h2 id="home-page">Home Page</h2>
<p>这是该网站的主页，可以看到我在<a href="https://github.com/Kim-Qian/">Github</a>上的动态及我最新的文章。</p>

<h2 id="categories">Categories</h2>
<p>在这里可以看到我分类好的文章。</p>

<h2 id="open-source">Open Source</h2>
<p>这是我在<a href="https://github.com/Kim-Qian/">Github</a>上的开源项目，会记录一些自己写的开源软件。</p>

<h2 id="wiki">Wiki</h2>
<p>这上面是在开发中遇到的一些常见问题的讲解。</p>

<h2 id="about-me">About me</h2>
<p>这上面有我常用的一些联系方式(请备注来意)。</p>

<h2 id="评论功能">评论功能：</h2>
<p>部分板块下设有评论区，每篇文章下也有。</p>

<p>评论区由 giscus 提供支持，可以登录自己的Github账号进行评论。</p>

<h1 id="我是怎么搭建的">我是怎么搭建的？</h1>
<p>该网站利用Github Pages，搭建在<a href="https://github.com/Kim-Qian/Kim-Qian.github.io">Github</a>上，欢迎各位大佬进行star和fork，如果有什么建议或者意见，欢迎在issue中提出，我会尽快进行回复。</p>

<h1 id="送上两句关于程序员名言我最喜欢的两句">送上两句关于程序员名言我最喜欢的两句：</h1>
<ul>
  <li>
    <p>Good artists copy, great artists steal.  ——Jobs</p>
  </li>
  <li>
    <p>Talk is cheap, show me the code.  ——Linus Torvalds</p>
  </li>
</ul>

<h1 id="谢幕">谢幕</h1>
<p>第一篇博客，markdown也不怎么会用，写出来的比较丑，慢慢来吧，一点儿一点儿的进步。</p>]]></content><author><name>Kim Qian</name></author><category term="Blog" /><summary type="html"><![CDATA[This Webside's Introduction]]></summary></entry><entry><title type="html">Python如何使用SQLite3？</title><link href="https://kim-qian.github.io//2023/08/10/python-sqlite3/" rel="alternate" type="text/html" title="Python如何使用SQLite3？" /><published>2023-08-10T00:00:00+08:00</published><updated>2023-08-10T00:00:00+08:00</updated><id>https://kim-qian.github.io//2023/08/10/python-sqlite3</id><content type="html" xml:base="https://kim-qian.github.io//2023/08/10/python-sqlite3/"><![CDATA[<p>SQLite3是一种轻量级、嵌入式的关系型数据库管理系统。它是一个C语言库，提供了SQL数据库引擎。SQLite3拥有小巧、高性能、可靠、自包含以及无服务器设计等特点。</p>

<h2 id="源码仓库e-notebook_sqlite"><a href="https://github.com/Kim-Qian/E-Notebook_SQLite">源码仓库E-Notebook_SQLite</a></h2>

<p>该程序实现了一个基于<a href="https://www.sqlite.org/index.html">sqlite3</a>的记事本功能。</p>

<p>支持写入，读取，修改，删除，查找，排序文本等功能，并能按多种方式显示结果。</p>

<h1 id="runoob"><a href="https://www.runoob.com/sqlite/sqlite-python.html">runoob</a></h1>

<h2 id="连接数据库">连接数据库</h2>
<p>下面的 Python 代码显示了如何连接到一个现有的数据库。如果数据库不存在，那么它就会被创建，最后将返回一个数据库对象。</p>

<pre><code class="language-Python">#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')

print ("数据库打开成功")
</code></pre>
<p>在这里，您也可以把数据库名称复制为特定的名称 :memory:，这样就会在 RAM 中创建一个数据库。现在，让我们来运行上面的程序，在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。保存上面代码到 sqlite.py 文件中，并按如下所示执行。如果数据库成功创建，那么会显示下面所示的消息：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Open database successfully
</code></pre></div></div>

<h2 id="创建表">创建表</h2>

<p>下面的 Python 代码段将用于在先前创建的数据库中创建一个表：</p>

<pre><code class="language-Python">#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
print ("数据库打开成功")
c = conn.cursor()
c.execute('''CREATE TABLE COMPANY
       (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL);''')
print ("数据表创建成功")
conn.commit()
conn.close()
</code></pre>

<p>上述程序执行时，它会在 test.db 中创建 COMPANY 表，并显示下面所示的消息：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>数据库打开成功
数据表创建成功
</code></pre></div></div>

<h2 id="insert-操作">INSERT 操作</h2>

<p>下面的 Python 程序显示了如何在上面创建的 COMPANY 表中创建记录：</p>

<pre><code class="language-Python">#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (1, 'Paul', 32, 'California', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (2, 'Allen', 25, 'Texas', 15000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )")

c.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
      VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )")

conn.commit()
print ("数据插入成功")
conn.close()
</code></pre>

<p>上述程序执行时，它会在 COMPANY 表中创建给定记录，并会显示以下两行：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>数据库打开成功
数据插入成功
</code></pre></div></div>

<h2 id="select-操作">SELECT 操作</h2>

<p>下面的 Python 程序显示了如何从前面创建的 COMPANY 表中获取并显示记录：</p>

<pre><code class="language-Python">#!/usr/bin/python
import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

cursor = c.execute("SELECT id, name, address, salary  from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print ("数据操作成功")
conn.close()
</code></pre>
<p>上述程序执行时，它会产生以下结果：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>数据库打开成功
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  20000.0

ID =  2
NAME =  Allen
ADDRESS =  Texas
SALARY =  15000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

数据操作成功
</code></pre></div></div>

<h2 id="update-操作">UPDATE 操作</h2>

<p>下面的 Python 代码显示了如何使用 UPDATE 语句来更新任何记录，然后从 COMPANY 表中获取并显示更新的记录：</p>

<pre><code class="language-Python">#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

c.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit()
print "Total number of rows updated :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary  from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print ("数据操作成功")
conn.close()
</code></pre>

<p>上述程序执行时，它会产生以下结果：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>数据库打开成功
Total number of rows updated : 1
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  25000.0

ID =  2
NAME =  Allen
ADDRESS =  Texas
SALARY =  15000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

数据操作成功
</code></pre></div></div>

<h2 id="delete-操作">DELETE 操作</h2>

<p>下面的 Python 代码显示了如何使用 DELETE 语句删除任何记录，然后从 COMPANY 表中获取并显示剩余的记录：</p>

<pre><code class="language-Python">#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()
print ("数据库打开成功")

c.execute("DELETE from COMPANY where ID=2;")
conn.commit()
print "Total number of rows deleted :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary  from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print ("数据操作成功")
conn.close()
</code></pre>

<p>上述程序执行时，它会产生以下结果：</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>数据库打开成功
Total number of rows deleted : 1
ID =  1
NAME =  Paul
ADDRESS =  California
SALARY =  20000.0

ID =  3
NAME =  Teddy
ADDRESS =  Norway
SALARY =  20000.0

ID =  4
NAME =  Mark
ADDRESS =  Rich-Mond
SALARY =  65000.0

数据操作成功
</code></pre></div></div>

<h1 id="进阶">进阶</h1>

<h2 id="改变select的排序方式">改变SELECT的排序方式</h2>

<pre><code class="language-Python">cursor = c.execute("SELECT created, title, notes, note_group FROM Data ORDER BY created DESC")
cursor = c.execute("SELECT created, title, notes, note_group FROM Data ORDER BY created ASC")
</code></pre>

<p>即在FROM XXX后加上 ORDER BY DESC或ASC</p>

<h2 id="select特定的数据如">SELECT特定的数据，如：</h2>

<pre><code class="language-Python">"SELECT * FROM students WHERE age &gt; ?"
</code></pre>
<p>就会查找students表中age栏目大于?的记录</p>

<h2 id="若发生错误则回滚事务">若发生错误，则回滚事务</h2>

<pre><code class="language-Python">try:
   cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Alice', 30))
   cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Bob', 25))
   conn.commit()
except:
   conn.rollback()
</code></pre>
<ul>
  <li>请用try, except包围</li>
</ul>

<h1 id="温馨提示">温馨提示</h1>

<ul>
  <li>
    <p>创建表格时最好写上</p>

    <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>IF NOT EXISTS
</code></pre></div>    </div>
  </li>
  <li>修改数据库完后一定要close()前commit()提交更改</li>
  <li>update时不要忘记加where条件，否则会更新所有数据，例如在想要更新数据库中一位用户的权限为管理员时，如果没有加上where条件，那么所有用户的权限都会变为管理员</li>
  <li>删除数据时不要忘记加where条件，否则会删除所有数据，例如在想要删除数据库中一位用户时，没有加上where条件，那么所有用户都会被删除</li>
  <li>占位符要用 ? , 而不是 %s, 一定要注意(?, ?, ?)中的占位符要与待插入的数据个数一致</li>
  <li>数据库操作完后一定要close()关闭数据库</li>
</ul>]]></content><author><name>Kim Qian</name></author><category term="Python" /><category term="SQLite3" /><summary type="html"><![CDATA[该文会比较详细的讲述Python如何使用自带的sqlite3模块。]]></summary></entry></feed>