Movatterモバイル変換


[0]ホーム

URL:


Open In App
Databases offer numerous functionalities by which one can manage large amounts of information easily over the web, and high-volume data input and output over a typical file such as a text file. SQL is a query language and is very popular in databases. Many websites use MySQL. SQLite is a "light" version that works over syntax very much similar to SQL.SQLite is a self-contained, high-reliability, embedded, full-featured, public-domain, SQL database engine. It is the most used database engine in the world wide web.Python has a library to access SQLite databases, called sqlite3, intended for working with this database which has been included with Python package since version 2.5.In this article we will discuss, how to query database using commands like Update and Delete and also to visualize data via graphs.It is recommended to go throughSQL using Python | Set 1

Updation and Deletion Operation

Python
# code for update operationimportsqlite3# database name to be passed as parameterconn=sqlite3.connect('mydatabase.db')# update the student recordconn.execute("UPDATE Student SET name = 'Sam' where unix='B113059'")conn.commit()print"Total number of rows updated :",conn.total_changescursor=conn.execute("SELECT * FROM Student")forrowincursor:printrow,conn.close()
Output:
Total number of rows updated : 1(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0), (u'B113058', u'Saan', u'2017-01-11 13:53:39', 21.0), (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
Python
# code for delete operationimportsqlite3# database name to be passed as parameterconn=sqlite3.connect('mydatabase.db')# delete student record from databaseconn.execute("DELETE from Student where unix='B113058'")conn.commit()print"Total number of rows deleted :",conn.total_changescursor=conn.execute("SELECT * FROM Student")forrowincursor:printrow,conn.close()
Output:
Total number of rows deleted : 1(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0), (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)

Data input by User

Python
# code for executing query using input dataimportsqlite3# creates a database in RAMcon=sqlite3.connect(":memory:")cur=con.cursor()cur.execute("create table person (name, age, id)")print("Enter 5 students names:")who=[raw_input()foriinrange(5)]print("Enter their ages respectively:")age=[int(raw_input())foriinrange(5)]print("Enter their ids respectively:")p_id=[int(raw_input())foriinrange(5)]n=len(who)foriinrange(n):# This is the q-mark style:cur.execute("insert into person values (?, ?, ?)",(who[i],age[i],p_id[i]))# And this is the named style:cur.execute("select * from person")# Fetches all entries from tableprintcur.fetchall()
Output:
(u'Navin', 34, 113053)(u'Basu', 42, 113058)(u'Firoz', 65, 113059)(u'Tim', 47, 113060)(u'Varun', 54, 113061)

Graphing with SQLite

Python
# graph visualization using matplotlib libraryimportmatplotlib.pyplotaspltdefgraph_data(p_id,age):# plotting the pointsplt.plot(p_id,age,color='yellow',linestyle='dashed',linewidth=3,marker='*',markerfacecolor='blue',markersize=12)# naming the x axisplt.xlabel('Persons Id')# naming the y axisplt.ylabel('Ages')# plt.plot(p_id,age)plt.show()print("Enter 5 students names:")who=[raw_input()foriinrange(5)]print("Enter their ages respectively:")age=[int(raw_input())foriinrange(5)]print("Enter their ids respectively:")p_id=[int(raw_input())foriinrange(5)]# calling graph functiongraph_data(p_id,age)
In this way we can perform such operations using SQL query to communicate with Database and plot a Graph significantly to draw out its characteristic.SQL using Python | Set 1SQL using Python | Set 3 (Handling large data)

Improve
Improve
Article Tags :

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp