read_sql का उपयोग
read_sql एक SQL query चलाता है और result को DataFrame के रूप में return करता है।
In this page:
Syntax
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
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. #}
आम गलतियां
- string formatting से queries बनाना
- connection को close करना भूल जाना
- 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: