Python 官方文档:入门教程 => 点击学习
1. 安装 MariaDB 的 Python 模块 sudo apt-get install python-pip Python-dev libMysqlclient-dev pip install mysql-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
--结束END--
本文标题: Python 操作 MariaDB 数据
本文链接: https://www.lsjlt.com/news/190169.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
一口价域名售卖能注册吗?域名是网站的标识,简短且易于记忆,为在线用户提供了访问我们网站的简单路径。一口价是在域名交易中一种常见的模式,而这种通常是针对已经被注册的域名转售给其他人的一种方式。
一口价域名买卖的过程通常包括以下几个步骤:
1.寻找:买家需要在域名售卖平台上找到心仪的一口价域名。平台通常会为每个可售的域名提供详细的描述,包括价格、年龄、流
443px" 443px) https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294.jpg https://www.west.cn/docs/wp-content/uploads/2024/04/SEO图片294-768x413.jpg 域名售卖 域名一口价售卖 游戏音频 赋值/切片 框架优势 评估指南 项目规模
0