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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值