查找MySQL数据库user表中是否存在ID=1的记录

查找MySQL数据库user表中是否存在ID=1的记录

如果直接查找该字段的值,会报错说该字段不存在(null)
所以应当按如下方式查找:(查找该ID的个数)

String SQL = "select count(*) from user where phone_id=?";		
count=jdbcTemplate.queryForInt(SQL,new Object[] {phone_id});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为你提供一些关于在 Pycharm 中实现 MySQL 数据库和设计一个 GUI 页面的指导。 首先,你需要安装 MySQL 数据库和 PyMySQL 模块。可以在终端中使用以下命令来安装: ``` pip install pymysql ``` 接下来,你需要在 MySQL 中创建一个数据库和一个表来存储学生信息。可以使用以下命令来创建: ```sql CREATE DATABASE student_db; USE student_db; CREATE TABLE student ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, age INT(11) NOT NULL, gender VARCHAR(255) NOT NULL, PRIMARY KEY (id) ); ``` 这里我们创建了一个名为 student_db 的数据库,并且在其中创建了一个名为 student 的表,包括 id、name、age 和 gender 四个字段。 接下来,你可以使用 Pycharm 中的 GUI 工具来设计一个 GUI 页面,用来添加、删除和查找学生信息。 可以使用 PyQt、Tkinter 等 GUI 工具包来实现,这里我以 PyQt5 为例来演示: 首先,你需要在 Pycharm 中创建一个 PyQt5 项目,然后在项目中创建一个 Python 文件,比如叫做 student_management_system.py。 然后,你可以在该文件中编写代码来实现 GUI 页面。具体来说,你需要设计一个窗口、标签、文本框、按钮等控件,并且为按钮添加事件响应函数,用来实现添加、删除和查找学生信息的功能。 比如: ```python import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QMessageBox import pymysql class StudentManagementSystem(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 300, 250) self.setWindowTitle('学生信息管理系统') self.name_label = QLabel('姓名:', self) self.name_label.move(20, 20) self.name_edit = QLineEdit(self) self.name_edit.move(80, 20) self.age_label = QLabel('年龄:', self) self.age_label.move(20, 60) self.age_edit = QLineEdit(self) self.age_edit.move(80, 60) self.gender_label = QLabel('性别:', self) self.gender_label.move(20, 100) self.gender_edit = QLineEdit(self) self.gender_edit.move(80, 100) self.add_button = QPushButton('添加', self) self.add_button.move(20, 150) self.add_button.clicked.connect(self.add_student) self.remove_button = QPushButton('删除', self) self.remove_button.move(100, 150) self.remove_button.clicked.connect(self.remove_student) self.find_button = QPushButton('查找', self) self.find_button.move(180, 150) self.find_button.clicked.connect(self.find_student) self.show() def add_student(self): name = self.name_edit.text() age = self.age_edit.text() gender = self.gender_edit.text() if name and age and gender: try: conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='student_db') cursor = conn.cursor() sql = "INSERT INTO student (name, age, gender) VALUES (%s, %s, %s)" cursor.execute(sql, [name, age, gender]) conn.commit() QMessageBox.information(self, '提示', '添加成功!') except: QMessageBox.warning(self, '警告', '添加失败!') finally: cursor.close() conn.close() else: QMessageBox.warning(self, '警告', '请输入完整信息!') def remove_student(self): name = self.name_edit.text() if name: try: conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='student_db') cursor = conn.cursor() sql = "DELETE FROM student WHERE name=%s" cursor.execute(sql, [name]) conn.commit() if cursor.rowcount > 0: QMessageBox.information(self, '提示', '删除成功!') else: QMessageBox.warning(self, '警告', '该学生不存在!') except: QMessageBox.warning(self, '警告', '删除失败!') finally: cursor.close() conn.close() else: QMessageBox.warning(self, '警告', '请输入学生姓名!') def find_student(self): name = self.name_edit.text() if name: try: conn = pymysql.connect(host='localhost', port=3306, user='root', password='123456', db='student_db') cursor = conn.cursor() sql = "SELECT * FROM student WHERE name=%s" cursor.execute(sql, [name]) result = cursor.fetchone() if result: QMessageBox.information(self, '学生信息', '姓名:{}\n年龄:{}\n性别:{}'.format(result[1], result[2], result[3])) else: QMessageBox.warning(self, '警告', '该学生不存在!') except: QMessageBox.warning(self, '警告', '查找失败!') finally: cursor.close() conn.close() else: QMessageBox.warning(self, '警告', '请输入学生姓名!') if __name__ == '__main__': app = QApplication(sys.argv) ex = StudentManagementSystem() sys.exit(app.exec_()) ``` 这里我们创建了一个 StudentManagementSystem 类,继承自 QWidget 类,用来表示我们的 GUI 窗口。然后我们在 initUI 方法中设计了窗口、标签、文本框、按钮等控件,并且为按钮添加了事件响应函数,用来实现添加、删除和查找学生信息的功能。 在按钮的事件响应函数中,我们使用 PyMySQL 模块来连接 MySQL 数据库,并且执行 SQL 语句来实现相应的功能。在执行 SQL 语句之前,我们需要先从文本框中获取用户输入的学生信息。 最后,在 if __name__ == '__main__' 中,我们创建了一个 QApplication 对象,并且将我们的 StudentManagementSystem 对象加入到该对象中,然后调用 app.exec_() 方法来启动 GUI 程序。 这里需要注意的是,我们在 SQL 语句中使用了格式化字符串来传递参数,这可以有效地防止 SQL 注入攻击。另外,在连接 MySQL 数据库之前,你需要先启动 MySQL 服务。 希望这些内容能够帮助到你,如有不足之处还请指正。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43751710

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值