Databases

Databases:
Database are popular for many applications, especially for use with web applications or customer-oriented programs.There is a caveat though; database don’t have the performance that file-system based application do.
Normal files,such as text files,are easy to create and use;Python has the tools built-in and it doesn’t take much to work with files.File systems are more efficient in terms of performance because you don’t have the overhead of database queries or other things to worry about.And files are easily portable between operating systems and are often editable/usable with different programs.

Working with database:
First off,consider a database to be one or more tables,just like a spreadsheet.The vertical columns comprises different fields or categories; they are analogous to the fields you fill out in a form.The horizontal rows are individual records;each row is one complete record entry.

Database Example

Creating an SQLite Database:
SQLite is built into Python,you simply import it like any other library .Once imported,you have to make a connection to it;
this creates the database file.A cursor is the object within SQLite that performs most of the functions you will be doing with the Database.

Pulling Data from a Database:
To retrieve the data from an SQLite Database,you just use the SQL commands that tell the Database what information you want and how you want it formatted.

1.Example for Returned data:

Knife,Small, 15
Machete, Medium,35
Axe,Large,55
Hatchet,Small,25
Hammer,Small,25
Screwdriver,Small,10
Prybar,Large,60
Knife,Small,15
Machete,Medium,35
Axe,Large,15
Hatchet,Small,25
Hammer,Small,25
Screwdriver,Small,10
Prybar,Large,60

        "Pretty printing" returned data
cursor.execute("Select*From Tools")
for row in cursor:
print "-" *10
print "ID:",row[0]
print "Name",row[1]
print "Size",row[2]
print "Price",row[3]
print "-"*10

OUTPUT:
ID:1
Name:Knife
Size:Small
Price:15

ID:2
Name:Machete
Size:Medium
Price:35

ID:3
Name:Axe
Size:Large
Price:55

ID:4
Name:Hatchet
Size:Small
Price:25

ID:5
Name:Hammer
Size:Small
Price:25

ID:6
Name:Screwdriver
Size:Small
Price:10

ID:7
Name:Prybar
Size:Large
Price:60

ID:8
Name:Knife
Size:Small
Price:15

ID:9
Name:Machete
Size:Medium
Price:35

ID:10
Name:Axe
Size:Large
Price:55

ID:11
Name:Hatchet
Size:Small
Price:25

ID:12
Name:Hammer
Size:Small
Price:25

ID:13
Name:Screwdriver
Size:Small
Price:10

ID:14
Name:Prybar
Size:Large
Price:60
Translate »