gpt4 book ai didi

Python:pandas read_sql_query

转载 作者:行者123 更新时间:2023-11-30 22:12:35 27 4
gpt4 key购买 nike

刚刚开始学习 Pandas 并偶然发现了“read_sql_query”:

在我的代码中有多个创建语句,并使用“read_sql_query”执行它们会使作业变得多余,例如:

import pandas as pd
import sqlite3

con = sqlite3.connect("/Users/sqlite_example.db3")

query_1 = ("Create table temp_1")
pd.read_sql_query(query_1,con)

query_2 = ("Create table temp_1")
pd.read_sql_query(query_2,con)

query_3 = ("Create table temp_3")
pd.read_sql_query(query_3,con)

我的问题是:

  1. 不能将所有查询都传递到单个“pd.read_sql_query”中
  2. 是否可以避免每次都打开与数据库的连接,即一次 con 参数
    还不够,还不如每次都通过?

我尝试查看文档和门户网站,但没有找到类似的内容。在“read_sql_query”中,每次“query,con”都必须传递,否则失败会给出错误。

请帮助理解传递多个查询并仅建立一次而不是多次连接的概念和方法。

我希望这能帮助其他在不久的将来可能面临类似问题的人。

最佳答案

这应该可以解决问题。

注意:executelist()用于在同一个查询中执行多个sql语句。

con = sqlite3.connect("/Users/sqlite_example.db3")
cursor=con.cursor()

query_1 = "Create table temp_1;
Create table temp_2;
Create table temp_3;"

cursor.executelist(query)

关于Python:pandas read_sql_query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51055000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com