数据库类型
数据库 | 命令 |
---|---|
MySQL | pip install PyMySQL |
Postgres | pip install psycopg2 |
Presto | pip install pyhive |
Oracle | pip install cx_Oracle |
sqlite | 短文本 |
Redshift | pip install sqlalchemy-redshift |
MSSQL | pip install pymssql |
Impala | pip install impyla |
SparkSQL | pip install pyhive |
mysql
pip install PyMySQL
1 |
|
插入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
#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost","testuser","test123","TESTDB" )
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# SQL 插入语句
sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
LAST_NAME, AGE, SEX, INCOME)
VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""
try:
# 执行sql语句
cursor.execute(sql)
# 提交到数据库执行
db.commit()
except:
# 如果发生错误则回滚
db.rollback()
# 关闭数据库连接
db.close()
查询
1 | #!/usr/bin/python3 |
Postgres
1 | import psycopg2 |
flask-sqlalchemy
Flask拥有丰富的扩展组件,数据库管理方面Flask-SQLAlchemy简化了数据库管理的操作
1 | pip install flask-sqlalchemy |
在以后的文章中我还会继续介绍