2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -4,7 +4,7 @@
In [1]:
```
```py
import sqlite3 as db
```
@@ -13,14 +13,14 @@ import sqlite3 as db
In [2]:
```
```py
connection = db.connect("my_database.sqlite")
```
不同的数据库有着不同的连接方法,例如 cx-oracle 数据库的链接方式为:
```
```py
connection = db.connect(username, password, host, port, 'XE')
```
@@ -28,7 +28,7 @@ connection = db.connect(username, password, host, port, 'XE')
In [3]:
```
```py
cursor = connection.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS orders(
order_id TEXT PRIMARY KEY,
@@ -46,7 +46,7 @@ connection.commit()
In [4]:
```
```py
orders = [
("A0002","2013-12-01","MSFT",1500,167.5),
("A0003","2013-12-02","GOOG",1500,167.5)
@@ -59,7 +59,7 @@ connection.commit()
cx-oracle 数据库使用不同的方式:
```
```py
cursor.executemany("""INSERT INTO orders VALUES
(:order_id, :date, :symbol, :quantity, :price)""",
orders)
@@ -69,20 +69,20 @@ orders)
In [5]:
```
```py
db.paramstyle
```
Out[5]:
```
```py
'qmark'
```
`query` 语句执行之后,我们需要进行 `commit`,否则数据库将不会接受这些变化,如果想撤销某个 `commit`,可以使用 `rollback()` 方法撤销到上一次 `commit()` 的结果:
```
```py
try:
... # perform some operations
except:
@@ -96,7 +96,7 @@ else:
In [6]:
```
```py
stock = 'MSFT'
cursor.execute("""SELECT *
FROM orders
@@ -107,7 +107,7 @@ for row in cursor:
```
```
```py
(u'A0002', u'2013-12-01', u'MSFT', 1500, 167.5)
```
@@ -116,7 +116,7 @@ for row in cursor:
In [7]:
```
```py
stock = 'AAPL'
cursor.execute("""SELECT *
FROM orders
@@ -128,7 +128,7 @@ cursor.fetchall()
Out[7]:
```
```py
[(u'A0001', u'2013-12-01', u'AAPL', 1000, 203.4)]
```
@@ -136,7 +136,7 @@ Out[7]:
In [8]:
```
```py
cursor.close()
connection.close()