Python 数据库和SQL
Python编程语言具有用于数据库编程的强大功能。Python支持各种数据库,例如SQLite,MySQL,Oracle,Sybase,PostgreSQL等。Python还支持数据定义语言(DDL),数据操作语言(DML)和数据查询语句。用于数据库接口的Python标准是Python DB-API。大多数Python数据库接口都遵守该标准。
以下是可用的Python数据库接口的列表:Python数据库接口和API。需要为访问的每个数据库下载单独的DB API模块。
在本章中,我们演示python编程语言连接SQLite数据库。这是通过使用python内置的sqlite3模块完成的。应该首先创建一个代表数据库的连接对象,然后创建一些游标对象以执行SQL语句。
连接到数据库
以下Python代码显示了如何连接到现有数据库。如果数据库不存在,则将创建该数据库,最后将返回一个数据库对象。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully";
在这里,还可以提供数据库名称作为特殊名称:memory:以在RAM中创建数据库。现在,我们运行上面的程序在当前目录中创建数据库test.db。可以根据需要更改路径,将上面的代码保存在sqlite.py文件中,并如下所示执行它。如果成功创建了数据库,则它将显示以下消息。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 $chmod +x sqlite.py $./sqlite.py Open database successfully
创建表
以下Python程序将用于在先前创建的数据库中创建表。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully"; conn.execute('''CREATE TABLE COMPANY (ID INT PRIMARY KEY not NULL, NAME TEXT not NULL, AGE INT not NULL, ADDRESS CHAR(50), SALARY REAL);''') print "Table created successfully"; conn.close()
执行上述程序后,它将在test.db中创建COMPANY表,并显示以下消息:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 Opened database successfully Table created successfully
插入操作
以下Python程序显示了如何在上面的示例中创建的COMPANY表中创建记录。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully"; conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ VALUES (1, 'Paul', 32, 'California', 19000.00 )"); conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ VALUES (2, 'Allen', 25, 'Texas', 19800.00 )"); conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ VALUES (3, 'Teddy', 23, 'Norway', 22000.00 )"); conn.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ VALUES (4, 'Mark', 25, 'Rich-Mond ', 55000.00 )"); conn.commit() print "Records created successfully"; conn.close()
执行上述程序后,它将在COMPANY表中创建给定记录,并将显示以下两行:
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 Opened database successfully Records created successfully
选择操作
以下Python程序演示了如何从上例中创建的COMPANY表中查询获取并显示记录。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully"; 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 "Operation done successfully"; conn.close()
执行上述程序时,将产生以下结果 :
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 Opened database successfully 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 Operation done successfully
更新操作
以下Python代码显示了如何使用UPDATE语句更新任何记录,然后从COMPANY表中查询获取并显示更新的记录。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully"; conn.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 "Operation done successfully"; conn.close()
执行上述程序时,将产生以下结果 :
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 Opened database successfully 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 Operation done successfully
删除操作
以下Python代码显示了如何使用DELETE语句删除任何记录,然后从COMPANY表中查询获取并显示其余记录。
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 #!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "Opened database successfully"; conn.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 "Operation done successfully"; conn.close()
执行上述程序时,将产生以下结果 :
# Filename : example.py # Copyright : 2020 By Codebaoku # Author by : www.yapf.com # Date : 2020-08-25 Opened database successfully 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 Operation done successfully
- python如何遍历字符串中每一个字符
- python中f字符串以及其常见用法介绍
- Python的gtts库将文字转为音频应该如何操作
- 使用Python Beautiful Soup解析HTML内容的方法
- python中的Pyperclip模块功能是什么
- Python异步之迭代器怎么使用
- Python异步之生成器怎么使用
- 如何使用Python点云生成3D网格
- Python如何利用手势识别实现贪吃蛇游戏
- Python网络爬虫之如何获取网络数据
- Python Internet 协议模块
- Python IP地址
- Python HTTP响应
- Python 网络接口
- Python HTTP客户端
- Python Telnet
- Python POP3
- Python 代理服务器
- Python列出目录
- Python 线程