def query_pic(cursor: Cursor, name: str): script = f'select resource from pic where code = "{name}"' data = list(query_mysql(cursor, script)) pic_resource = data[0][0] if len(data) == 0: return None else: img = cv.imread(pic_resource) cv.imshow('edge', img) cv.waitKey() return data
时间: 2024-04-19 20:24:59 浏览: 22
这是一个用于查询数据库中图片资源路径并显示图片的函数。它接受一个数据库游标和一个股票代码作为参数。首先,它构建一个SQL查询语句,根据股票代码查询对应的图片资源路径。然后,它执行查询,并获取查询结果。如果查询结果为空,表示没有对应的图片资源,函数返回None。否则,它使用OpenCV库读取图片,并显示在窗口中。最后,它返回查询结果。
你有其他问题吗?
相关问题
if "__main__" == __name__:
If "__main__" == __name__:, it is a conditional statement commonly used in Python scripts. It checks if the current module is being run directly as the main program. In other words, it ensures that certain code is only executed when the module is run directly and not when it is imported as a module in another script.
This check is useful when you have some code that should only run when the script is executed directly, such as initializing variables, defining functions, or running specific operations. When the module is imported, this code block will be skipped.
Here's an example to illustrate its usage:
```python
def main():
# code to be executed when the script is run directly
pass
if "__main__" == __name__:
main()
```
In this example, the `main()` function will only be executed if the script is run directly. If the module is imported by another script, the `main()` function will not be executed.
最后用if__name__ == '__main__': main()
好的,下面是加入 `if __name__ == '__main__':` 的完整代码:
```python
import sqlite3
def create_table():
conn = sqlite3.connect('courses.db')
c = conn.cursor()
# 创建数据表
c.execute('''CREATE TABLE courses
(name TEXT, teacher TEXT, location TEXT, weekday TEXT, start_time INT, end_time INT)''')
conn.commit()
conn.close()
def add_course(name, teacher, location, weekday, start_time, end_time):
conn = sqlite3.connect('courses.db')
c = conn.cursor()
# 添加一门课程
c.execute(f"INSERT INTO courses VALUES ('{name}', '{teacher}', '{location}', '{weekday}', {start_time}, {end_time})")
conn.commit()
conn.close()
def view_courses():
conn = sqlite3.connect('courses.db')
c = conn.cursor()
# 查询所有课程信息
c.execute("SELECT * FROM courses")
rows = c.fetchall()
for row in rows:
print(row)
conn.close()
def edit_course(name, teacher, location, weekday, start_time, end_time):
conn = sqlite3.connect('courses.db')
c = conn.cursor()
# 修改一门课程
c.execute(f"UPDATE courses SET teacher='{teacher}', location='{location}', weekday='{weekday}', start_time={start_time}, end_time={end_time} WHERE name='{name}'")
conn.commit()
conn.close()
def delete_course(name):
conn = sqlite3.connect('courses.db')
c = conn.cursor()
# 删除一门课程
c.execute(f"DELETE FROM courses WHERE name='{name}'")
conn.commit()
conn.close()
def main():
# 创建数据表
create_table()
# 添加一门课程
add_course('计算机网络', '张三', '教学楼101', '星期一', 8, 10)
# 查看所有课程
view_courses()
# 修改一门课程
edit_course('计算机网络', '李四', '教学楼201', '星期二', 10, 12)
# 查看所有课程
view_courses()
# 删除一门课程
delete_course('计算机网络')
# 查看所有课程
view_courses()
if __name__ == '__main__':
main()
```
这个代码实现了创建数据表、添加课程、查看课程、编辑课程、删除课程等功能,并使用了 `if __name__ == '__main__':` 来保证了代码的可重用性和可扩展性。你可以根据需要进行修改和扩展,让其更加符合你的实际需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)