← Back to Pandas Course | Chapter 11: File I/O | Lesson 4 of 7

read_sql का उपयोग

read_sql एक SQL query चलाता है और result को DataFrame के रूप में return करता है।

In this page:

  1. read_sql
Syntax
python
df = pd.read_sql("SELECT * FROM table_name", connection)

read_sql

pd.read_sql(query, connection) sqlite3 जैसे DBAPI connection या SQLAlchemy engine के साथ काम करता है। safe parameter binding के लिए params का उपयोग करें। इसका उल्टा, to_sql, DataFrame को एक table में लिखता है।

Note: string formatting से SQL बनाने के बजाय parameters (params=) का उपयोग करें।

उदाहरण: read_sql

python
import sqlite3
import pandas as pd

con = sqlite3.connect(":memory:")
pd.DataFrame({"id": [1, 2, 3], "name": ["Ann", "Bob", "Cy"]}).to_sql("users", con, index=False)
print(pd.read_sql("SELECT * FROM users WHERE id > ?", con, params=(1,)))
con.close()

# Output:
#    id name
# 0   2  Bob
# 1   3   Cy
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. string formatting से queries बनाना
  2. connection को close करना भूल जाना
  3. to_sql में if_exists specify न करना
चैप्टर सारांश
  • read_sql query को DataFrame में चलाता है
  • sqlite3 connections के साथ काम करता है
  • to_sql tables लिखता है
  • safety के लिए params का उपयोग करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.