We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Homework5不在终端中输入SQL语句然后将结果分别截图,直接利用一些python的库来在notebook里面运行SQL代码可以吗? 比如说可以这样做:
import mysql.connector # 连接到MySQL服务器 connection = mysql.connector.connect( host="localhost", user="root", password="password" #替换为你的密码 ) cursor = connection.cursor() cursor.execute("CREATE DATABASE IF NOT EXISTS test_database") cursor.execute("USE test_database")
然后第一题是在新数据库中新建一张 user 表,插入几条数据:
# 创建 user 表 create_table_query = """ CREATE TABLE IF NOT EXISTS user ( id INT PRIMARY KEY, name VARCHAR(255), sex VARCHAR(10), age INT, phone VARCHAR(20) ); """ cursor.execute(create_table_query) # 插入数据 insert_data_query = """ INSERT INTO user (id, name, sex, age, phone) VALUES (1, 'John Doe', 'Male', 25, '123-456-7890'), (2, 'Jane Smith', 'Female', 31, '987-654-3210'), (3, 'Bob Johnson', 'Male', 22, '555-123-4567'), (4, 'Taylor Swift', 'Female', 17, '555-123-4568'); """ cursor.execute(insert_data_query) select_query = "SELECT * FROM user;" cursor.execute(select_query) results = cursor.fetchall() for row in results: print(row)
最后关闭连接:
# 关闭连接 cursor.close() connection.close()
当然以上只是演示,实际也是能跑通(需要pip install mysql-connector)然后输出正确结果在notebook里的,使用pymysql库以及ORM也同理,请问这样可以吗?
The text was updated successfully, but these errors were encountered:
可以的
Sorry, something went wrong.
No branches or pull requests
Homework5不在终端中输入SQL语句然后将结果分别截图,直接利用一些python的库来在notebook里面运行SQL代码可以吗?
比如说可以这样做:
然后第一题是在新数据库中新建一张 user 表,插入几条数据:
最后关闭连接:
当然以上只是演示,实际也是能跑通(需要pip install mysql-connector)然后输出正确结果在notebook里的,使用pymysql库以及ORM也同理,请问这样可以吗?
The text was updated successfully, but these errors were encountered: