Python处理MySQL与SQLite数据库详解!
Python处理MySQL与SQLite数据库详解!
在数据处理和存储方面,数据库扮演着至关重要的角色,Python提供了多种与数据库交互的方式,本文主要介绍了如何对MySQL与SQLite数据库进行增删改查操作,需要的可以了解下。
在数据处理和存储方面,数据库扮演着至关重要的角色。Python提供了多种与数据库交互的方式,其中pymysql库用于连接和操作MySQL数据库,而SQLite则是一种轻量级的嵌入式数据库,Python标准库中的sqlite3模块即可满足操作需求。本文将为入门者介绍MySQL和SQLite,并分别展示如何使用Python进行增删改查操作。
1. MySQL 简介
MySQL是一种开源的关系型数据库管理系统(RDBMS),广泛应用于各种Web应用。它支持标准的SQL语言,提供了高性能、高可靠性和可扩展性。
安装pymysql
在Python中操作MySQL数据库,需要先安装pymysql库。可以使用pip进行安装:
1
|
pip install pymysql |
1.1 连接MySQL数据库
1
2
3
4
5
6
7
8
9
10
11
12
|
import pymysql # 创建数据库连接 connection = pymysql.connect( host = 'localhost' , # 数据库主机地址 user = 'your_username' , # 数据库用户名 password = 'your_password' , # 数据库密码 database = 'your_database' # 数据库名称 ) # 创建游标对象 cursor = connection.cursor() |
1.2 增加数据(Insert)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
try : # SQL插入语句 sql = "INSERT INTO your_table (column1, column2) VALUES (%s, %s)" val = ( "value1" , "value2" ) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "插入成功" ) except Exception as e: print ( "插入失败:" , e) # 回滚事务 connection.rollback() finally : # 关闭游标和连接 cursor.close() connection.close() |
1.3 查询数据(Select)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
try : # SQL查询语句 sql = "SELECT * FROM your_table" # 执行SQL语句 cursor.execute(sql) # 获取所有记录 results = cursor.fetchall() for row in results: print (row) except Exception as e: print ( "查询失败:" , e) finally : # 关闭游标和连接 cursor.close() connection.close() |
1.4 更新数据(Update)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
try : # SQL更新语句 sql = "UPDATE your_table SET column1 = %s WHERE column2 = %s" val = ( "new_value1" , "value2" ) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "更新成功" ) except Exception as e: print ( "更新失败:" , e) # 回滚事务 connection.rollback() finally : # 关闭游标和连接 cursor.close() connection.close() |
1.5 删除数据(Delete)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
try : # SQL删除语句 sql = "DELETE FROM your_table WHERE column1 = %s" val = ( "value1" ,) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "删除成功" ) except Exception as e: print ( "删除失败:" , e) # 回滚事务 connection.rollback() finally : # 关闭游标和连接 cursor.close() connection.close() |
2. SQLite 简介
SQLite是一种轻量级的嵌入式数据库,它不需要单独的服务器进程,非常适合在本地存储数据。SQLite支持标准的SQL语法,并且Python标准库中的sqlite3模块可以直接操作SQLite数据库。
2.1 连接SQLite数据库
1
2
3
4
5
6
7
|
import sqlite3 # 创建数据库连接(如果数据库文件不存在会自动创建) connection = sqlite3.connect( 'your_database.db' ) # 创建游标对象 cursor = connection.cursor() |
2.2 增加数据(Insert)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
try : # SQL插入语句 sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)" val = ( "value1" , "value2" ) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "插入成功" ) except sqlite3.Error as e: print ( "插入失败:" , e) finally : # 关闭游标和连接 cursor.close() connection.close() |
2.3 查询数据(Select)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
try : # SQL查询语句 sql = "SELECT * FROM your_table" # 执行SQL语句 cursor.execute(sql) # 获取所有记录 results = cursor.fetchall() for row in results: print (row) except sqlite3.Error as e: print ( "查询失败:" , e) finally : # 关闭游标和连接 cursor.close() connection.close() |
2.4 更新数据(Update)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
try : # SQL更新语句 sql = "UPDATE your_table SET column1 = ? WHERE column2 = ?" val = ( "new_value1" , "value2" ) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "更新成功" ) except sqlite3.Error as e: print ( "更新失败:" , e) finally : # 关闭游标和连接 cursor.close() connection.close() |
2.5 删除数据(Delete)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
try : # SQL删除语句 sql = "DELETE FROM your_table WHERE column1 = ?" val = ( "value1" ,) # 执行SQL语句 cursor.execute(sql, val) # 提交事务 connection.commit() print ( "删除成功" ) except sqlite3.Error as e: print ( "删除失败:" , e) finally : # 关闭游标和连接 cursor.close() connection.close() |
通过以上步骤,你可以轻松地使用Python对MySQL和SQLite数据库进行增删改查操作。
以上就是Python处理MySQL与SQLite数据库详解的详细内容。
学习资料见知识星球。
以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。
快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利!
更多技巧, www.excelbook.cn
欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;
1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。
共有 0 条评论