

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to read sample data from an SQL Database
Assume you have a sqlite3 database with student records and the result for reading all the data is,
Id Name 0 1 stud1 1 2 stud2 2 3 stud3 3 4 stud4 4 5 stud5
Solution
To solve this, we will follow the steps given below −
Define a new connection. It is shown below,
con = sqlite3.connect("db.sqlite3")
Read sql data from the database using below function,
pd.read_sql_query()
Select all student data from table using read_sql_query with connection,
pd.read_sql_query("SELECT * FROM student", con)
Example
Let us see the complete implementation to get a better understanding −
import pandas as pd import sqlite3 con = sqlite3.connect("db.sqlite3") df = pd.read_sql_query("SELECT * FROM student", con) print(df)
Output
Id Name 0 1 stud1 1 2 stud2 2 3 stud3 3 4 stud4 4 5 stud5
- Related Questions & Answers
- Write a Python program to read an Excel data from file and read all rows of first and last columns
- Write a C program to read a data from file and display
- How to read/write data from/to .properties file in Java?
- How to read/retrieve data from Database to JSON using JDBC?
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Database READ Operation in Python
- Write a Python code to read JSON data from a file and convert it to dataframe, CSV files
- How to create a file, write data into it and read data from it on iOS?
- Python program to read input from console
- How can I write an SQL IN query with a Python tuple?
- How to read data from scanner to an array in java?
- How to randomly sample rows from an R data frame using sample_n?
- Read and Write to an excel file using Python openpyxl module
- Run SQL file in MySQL database from terminal?
- Read/Write Class Objects from/to File in C++
Advertisements