python的sqlite3的sql语句大全
时间: 2024-09-28 08:16:20 浏览: 50
Python操作sqlite3快速、安全插入数据(防注入)的实例
SQLite3是Python标准库中用于操作SQLite数据库的一个模块,它支持一系列SQL语句来增删改查数据。以下是一些常见的SQLite3 SQL语句:
1. **创建数据库** (CREATE DATABASE)
```sql
CREATE DATABASE my_database;
```
2. **连接到数据库** (CONNECT)
```python
import sqlite3
conn = sqlite3.connect('my_database.db')
```
3. **创建表** (CREATE TABLE)
```sql
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
email VARCHAR(50) UNIQUE
);
```
4. **插入数据** (INSERT INTO)
```sql
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
```
5. **查询数据** (SELECT)
```sql
SELECT * FROM users WHERE email = 'john@example.com';
```
6. **更新数据** (UPDATE)
```sql
UPDATE users SET name = 'Jane Doe' WHERE id = 1;
```
7. **删除数据** (DELETE FROM)
```sql
DELETE FROM users WHERE id = 1;
```
8. **创建视图** (CREATE VIEW)
```sql
CREATE VIEW user_emails AS SELECT name, email FROM users;
```
9. **删除视图** (DROP VIEW)
```sql
DROP VIEW user_emails;
```
10. **事务管理** (BEGIN/COMMIT/ROLLBACK)
```python
conn.execute("BEGIN TRANSACTION")
# 执行一些SQL...
conn.commit() # 提交
```
阅读全文