前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 操作 MariaDB 数据

Python 操作 MariaDB 数据

作者头像
py3study
发布2020-01-07 16:40:51
1.8K0
发布2020-01-07 16:40:51
举报
文章被收录于专栏:python3python3

1. 安装 MariaDB 的 Python 模块

代码语言:javascript
复制
sudo apt-get install python-pip python-dev libmysqlclient-dev
pip install MySQL-python

我们需要导入 MariaDB 的 Python 模块,即 import MySQLdb 才能使用 python 对 MariaDB进行数据的增删减查等操作。

2. Python 操作 MariaDB 的入门例子

代码语言:javascript
复制
#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect(host='127.0.0.1'  
                       ,user='your username'  
                       ,passwd='your password'  
                       ,db='information_schema')  

# prepare a cursor object using cursor() method
cursor = conn.cursor()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print "Database version : %s " % data

# disconnect from server
conn.close()

输出 MariaDB 的版本信息,表示成功通过 python 对 MariaDB 进行查询操作。

这里写图片描述
这里写图片描述

那么,该怎么用 Python 对 MariaDB 数据库进行增删查等功能呢?下面是一些例子:

3. 创建Table

代码语言:javascript
复制
#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect(host='127.0.0.1'  
                       ,user='your username'  
                       ,passwd='your password'  
                       ,db='TEST')  

# prepare a cursor object using cursor() method
cursor = conn.cursor()

# create a table
cursor.execute("DROP TABLE IF EXISTS MENU")
sql = """CREATE TABLE MENU (ORDERS  CHAR(20) NOT NULL)"""
cursor.execute(sql)

# disconnect from server
conn.close()

4. 插入记录

代码语言:javascript
复制
#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect(host='127.0.0.1'  
                       ,user='your username'  
                       ,passwd='your password'  
                       ,db='TEST')  

# prepare a cursor object using cursor() method
cursor = conn.cursor()

# Prepare SQL query to INSERT a record into the database.
sql = """INSERT INTO MENU(ORDERS) VALUES ('O1')"""

try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   conn.commit()
except:
   # Rollback in case there is any error
   conn.rollback()

# disconnect from server
conn.close()

5. 查看数据

代码语言:javascript
复制
#!/usr/bin/python

import MySQLdb

# Open database connection
conn = MySQLdb.connect(host='127.0.0.1'  
                       ,user='your username'  
                       ,passwd='your password'  
                       ,db='TEST')  

# prepare a cursor object using cursor() method
cursor = conn.cursor()


sql = "SELECT * FROM MENU"

try:
   # Execute the SQL command
   cursor.execute(sql)
   # Fetch all the rows in a list of lists.
   results = cursor.fetchall()
   for row in results:
      orders = row[0]
      # Now print fetched result
      print "%s" % (orders)

except:
   # Rollback in case there is any error
   print 'unable to fetch data'

# disconnect from server
conn.close()

参考 https://www.tutorialspoint.com/python/python_database_access.htm

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档