Python 操作 MariaDB 数据库

本文介绍了如何使用Python操作MariaDB数据库,包括安装Python模块、查询版本、创建表、插入记录和查询数据等基本操作。

1. 安装 MariaDB 的 Python 模块

sudo apt-get install python-pip python-dev libmysqlclient-dev
pip install MySQL-python

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

2. Python 操作 MariaDB 的入门例子

#!/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

#!/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. 插入记录

#!/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. 查看数据

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值