Python:pandas read_sql_查询

2024-04-23 19:39:08 发布

您现在位置:Python中文网/ 问答频道 /正文

刚开始学熊猫,就在“读sql”的时候摔倒了:

在我的代码中有多个create语句,使用“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_查询““
  2. 是否可以避免每次打开与数据库的连接,即一次con参数
    不是每次都通过就足够了吗?在

我试着查看文档和门户网站,但没有找到这样的东西。在“read_sql_query”中,每次必须传递“query,con”,否则如果不这样做,则会出现错误。在

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

我希望这也能帮助那些在不久的将来面临类似问题的人。在


Tags: 代码importpandasreadsqlcreate作业table
1条回答
网友
1楼 · 发布于 2024-04-23 19:39:08

这应该能奏效。在

{cd1>中使用多个语句执行同一个查询。在

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)

相关问题 更多 >