python连接数据库实验 中山大学数据库实验8

数据库实验 8 — Database Access From a Programming Language

一、实验内容

This lab introduces you to database access from a programming language such as Java or C#. A sample set of exercises can be found here; although phrased using Java/JDBC, the exercise can be done using other languages, OBDC or ADO.NET APIs.In this assignment, you will write a java program which takes any of the following commands as command line argument (not as input), and executes them using JDBC:

  1. insert into relationname value1 value2 value3 …

    Inserts a tuple into the specified relation with the specified values; make sure you use a prepared statement; test it with input containing single quotes (if you run from command line, enclose those in double quotes so linux doesn’t interpret the quotes) you can assume that all values are strings for simplicity.

  2. select from relationname

    Prints all tuples from the specified relation. For the purpose of this assignment, you should assume that relationname is one of “instructor”, “student” or “takes”, and not use database metadata features to print the tuples.

  3. select from relationname where “condition”

    Executes a query with the specified condition. Note the use of double quotes so that the condition comes as a single command line parameter. Again assume relationname is one of those from the previous feature. So this is really a small addition to the code for the previous feature, do NOT make a separate copy of the code.

  4. select from relationname1 relationname

    Displays result of natural join of the two relations. This time, use the resultset metadata feature to display all values from the query result; don’t worry about displaying column names, that is optional, we only care about values being displayed.

二、实验过程及结果

本次实验采用python进行数据库的连接和各种操作!
1、安装pymysql

指定清华镜像源安装(注意关掉VPN):

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pymysql==0.9.3	 
2、用python编写连接数据库的代码
import pymysql

#连接数据库,
conn=pymysql.connect(
    host = '127.0.0.1', # 连接名称,默认127.0.0.1
    user = 'root', # 用户名
    passwd='password', # 密码
    port= 3306, # 端口,默认为3306
    db='database', # 数据库名称
    charset='utf8' # 字符编码
)

# 建游标对象
cur = conn.cursor()

# 输入mysql的语句
#sql = input()
## 以“;”结束语句输入
sql = ''
s = input()
while True: 
    sql = sql + s + '\n'
    if s[-1] == ';':
        break
    s = input('-> ')
print(sql)

try:
    cur.execute(sql) # 执行插入的sql语句
    data = cur.fetchall() # 通过fetchall方法获得数据
    for i in data:
        print(i)
    
    conn.commit() # 提交到数据库执行
    
except:
    conn.rollback()# 如果发生错误则回滚

# 关闭游标
cur.close()
# 关闭连接
conn.close()

3、实验结果
(1)向特定关系插入元组

在这里插入图片描述

(2)从指定的关系中打印所有元组。

在这里插入图片描述

(3)使用指定的条件执行查询。

单引号和双引号都适用:
在这里插入图片描述
在这里插入图片描述

(4)显示两个关系的自然连接的结果。

在这里插入图片描述
但是我还是不会写java…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面我将为你介绍Python连接MySQL数据库实验过程。 在开始实验前,需要先安装MySQLPythonMySQL驱动程序。假设你已经安装好了这些程序,下面是实验的具体步骤: 1. 导入MySQL驱动程序 首先,在Python代码中导入MySQL驱动程序,代码如下: ``` import pymysql ``` 2. 建立数据库连接 在导入MySQL驱动程序后,需要建立数据库连接连接数据库需要指定数据库的地址、用户名、密码、数据库名等信息。代码如下: ``` # 建立数据库连接 conn = pymysql.connect( host='localhost', user='root', password='123456', database='test', port=3306, charset='utf8' ) ``` 其中,host指定了数据库的地址,user和password分别指定了用户名和密码,database指定了要连接数据库名,port指定了连接MySQL数据库的端口号,charset指定了连接的字符集。 3. 创建游标对象 连接成功后,需要创建游标对象,游标是用来执行SQL语句的。代码如下: ``` # 创建游标对象 cursor = conn.cursor() ``` 4. 执行SQL语句 创建游标对象后,就可以执行SQL语句了。下面是一个简单的例子,查询test数据库中的student表中的所有数据: ``` # 执行SQL语句 sql = 'SELECT * FROM student' cursor.execute(sql) # 获取查询结果 result = cursor.fetchall() # 输出查询结果 for row in result: print(row) ``` 5. 关闭数据库连接 在查询完毕后,需要关闭数据库连接。代码如下: ``` # 关闭游标对象和数据库连接 cursor.close() conn.close() ``` 完整代码如下: ``` import pymysql # 建立数据库连接 conn = pymysql.connect( host='localhost', user='root', password='123456', database='test', port=3306, charset='utf8' ) # 创建游标对象 cursor = conn.cursor() # 执行SQL语句 sql = 'SELECT * FROM student' cursor.execute(sql) # 获取查询结果 result = cursor.fetchall() # 输出查询结果 for row in result: print(row) # 关闭游标对象和数据库连接 cursor.close() conn.close() ``` 以上就是Python连接MySQL数据库实验过程。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值