跳转至

使用python向SQLite数据库插入数据

由于默认情况下一般是自带sqlite3 python包的,所以无需安装,可以直接插入数据。下面是一个示例 Python 脚本,创建一个 SQLite 数据库,并向其中插入 10 条数据,每条数据包含 SVG 图片的源码信息。

1. 创建 SQLite 数据库

创建一个名为 example.db 的 SQLite 数据库,并创建一个表 Images 用于存储图片信息。每条数据包含图片的名称和 SVG 图片的源码。

2. 插入数据

插入 10 条包含 SVG 图片源码的示例数据。

以下是完整的 Python 脚本:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sqlite3

# 连接到 SQLite 数据库(如果数据库不存在,将会创建一个新的数据库)
conn = sqlite3.connect('example.db')
cursor = conn.cursor()

# 创建一个表用于存储图片信息
cursor.execute('''
CREATE TABLE IF NOT EXISTS Images (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    data TEXT NOT NULL
)
''')

# 插入 10 条包含 SVG 图片源码的示例数据
svg_data = [
    ('image1', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>'),
    ('image2', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect width="100" height="100" style="fill:blue" /></svg>'),
    ('image3', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><polygon points="50,15 90,85 10,85" style="fill:lime;stroke:purple;stroke-width:1" /></svg>'),
    ('image4', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><ellipse cx="50" cy="50" rx="50" ry="25" style="fill:yellow;stroke:purple;stroke-width:2" /></svg>'),
    ('image5', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><line x1="0" y1="0" x2="100" y2="100" style="stroke:rgb(255,0,0);stroke-width:2" /></svg>'),
    ('image6', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><text x="10" y="40" fill="black">Hello SVG</text></svg>'),
    ('image7', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><polyline points="0,40 40,40 40,80 80,80 80,120 120,120" style="fill:white;stroke:black;stroke-width:2" /></svg>'),
    ('image8', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><path d="M10 10 H 90 V 90 H 10 L 10 10" style="fill:none;stroke:black;stroke-width:3" /></svg>'),
    ('image9', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><circle cx="50" cy="50" r="40" style="fill:none;stroke:green;stroke-width:4" /></svg>'),
    ('image10', '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"><rect x="10" y="10" width="80" height="80" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" /></svg>')
]

# 执行插入操作
cursor.executemany('INSERT INTO Images (name, data) VALUES (?, ?)', svg_data)

# 提交事务
conn.commit()

# 关闭数据库连接
conn.close()

print("Data inserted successfully.")

说明

  1. 连接数据库:使用 sqlite3.connect('example.db') 连接到 SQLite 数据库。如果数据库文件不存在,将会创建一个新的数据库文件。
  2. 创建表:使用 CREATE TABLE IF NOT EXISTS Images 创建一个包含 id(自增主键)、name(图片名称)和 data(SVG 图片源码)字段的表。
  3. 插入数据:使用 cursor.executemany 方法批量插入 10 条包含 SVG 图片源码的示例数据。
  4. 提交事务和关闭连接:使用 conn.commit() 提交事务,并使用 conn.close() 关闭数据库连接。

运行此脚本后,example.db 数据库将包含 10 条示例数据,每条数据包含一个 SVG 图片的源码。

捐赠本站(Donate)

weixin_pay
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))