python连接数据库

#%%
import pymysql
#  插入语句
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 数据列表
data = [(1,'张三','18'),
        (2,'李四','19'),
        (3,'王五','20'),
        ]
try:
    # 执行sql语句,插入多条数据
    cursor.executemany("insert into test(id, name, score) values (%s,%s,%s)", data)
    # 提交数据
    db.commit()
except:
    # 发生错误时回滚
    db.rollback()
# 关闭数据库连接
db.close()
# ======================================================================================== #
#%%
# 导入pymysql模块
import pymysql
# 查询单个数据
# 连接database,参数1主机名或IP;参数2:用户名;参数3:密码;参数4:数据库名称
conn = pymysql.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 创建一个可以执行SQL语句的光标对象
cursor = conn.cursor()
# 定义要执行的SQL语句
sql = """
select * from test
"""
# 执行SQL语句
cursor.execute(sql)
# 查询单条数据
data = cursor.fetchone()
print(data);
# 关闭光标对象
cursor.close()
# 关闭数据库连接
conn.close()
# ======================================================================================== #
# %%
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 插入语句
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
# SQL 插入语句
sql = """INSERT into test(id,name,score)value (4,'qqq','aaa')"""
try:
   # 执行sql语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
except:
   # Rollback in case there is any error
   db.rollback()
# 关闭数据库连接
db.close()
# ======================================================================================== #
# %%
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 查询语句
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
# SQL 查询语句
sql = "SELECT * FROM test \
       WHERE id >= %s" % (1)
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 获取所有记录列表
   results = cursor.fetchall()
   for row in results:
      id = row[0]
      name = row[1]
      score = row[2]
      # 打印结果
      print ("id=%s,name=%s,score=%s" % \
             (id, name, score ))
except:
   print ("Error: unable to fecth data")
# 关闭数据库连接
db.close()
# ======================================================================================== #
# %%
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 跟新语句
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
# SQL 更新语句
sql = "UPDATE test SET id = id + 100 WHERE name = '%s'" % \
     ("aaa")
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 提交到数据库执行
   db.commit()
except:
   # 发生错误时回滚
   db.rollback()
# 关闭数据库连接
db.close()
# ======================================================================================== #
# %%
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 删除语句
import MySQLdb
# 打开数据库连接
db = MySQLdb.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标 
cursor = db.cursor()
# SQL 删除语句
sql = "DELETE FROM test WHERE id = %s" % (201)
try:
   # 执行SQL语句
   cursor.execute(sql)
   # 提交修改
   db.commit()
except:
   # 发生错误时回滚
   db.rollback()
# 关闭连接
db.close()
# ======================================================================================== #
# %%
# -*- coding:utf-8 -*-
# 查询mysql版本
import MySQLdb
# 打开数据库连接(请根据自己的用户名、密码及数据库名称进行修改)
cnn = MySQLdb.connect(host="localhost", user="root", password="root", database="python",charset="utf8")
# 使用cursor()方法获取操作游标 
cursor = cnn.cursor()
# 使用execute方法执行SQL语句
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取一条数据
data = cursor.fetchone()
print ("Database version : %s " % data)
# 执行sql语句
cnn.close() 
# %%
以上是 python连接数据库 的全部内容, 来源链接: utcz.com/z/388209.html






