#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost", "root", "123456", "test")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print("Database version : %s " % data)
# 使用预处理语句创建表
# sql = """CREATE TABLE EMPLOYEE (
# FIRST_NAME CHAR(20) NOT NULL,
# LAST_NAME CHAR(20),
# AGE INT,
# SEX CHAR(1),
# INCOME FLOAT )"""
#
# cursor.execute(sql)
# # SQL 插入语句
# sql = """INSERT INTO EMPLOYEE(FIRST_NAME,
# LAST_NAME, AGE, SEX, INCOME)
# VALUES ('Mac1', 'Mohan', 20, 'M', 2000)"""
#
# sql2 = """INSERT INTO EMPLOYEE(FIRST_NAME,
# LAST_NAME, AGE, SEX, INCOME)
# VALUES ('Mac2', 'Mohan', 20, 'M', 2000)"""
# try:
# # 执行sql语句
# cursor.execute(sql)
# cursor.execute(sql2)
# # 提交到数据库执行
# db.commit()
# except:
# # 如果发生错误则回滚
# db.rollback()
# SQL 查询语句
sql = "SELECT * FROM EMPLOYEE WHERE FIRST_NAME='Mac'"
try:
# 执行SQL语句
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
# results = cursor.fetchone()
print(results)
for row in results:
print(row)
fname = row[0]
lname = row[1]
age = row[2]
sex = row[3]
income = row[4]
# 打印结果
print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
(fname, lname, age, sex, income ))
except:
print ("Error: unable to fetch data")
# 关闭数据库连接
db.close()
python mysql 示例
最新推荐文章于 2024-11-02 16:28:26 发布