Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
(★ Dяea Tutorials ★ )-==-[ DataBases in Python ]-==-(★Tutorial no.1★)
#1
Intro

Well boys and girls, i searched python forum to see what if someone post a Tut about databases in python and i got 0, all i got are questions, so i though of making a Tutorial about making databases in python.

What is the Database?


Database is a collection of well formed and organized data that u can use and make good things.

For more Info: Wiki

[Image: Line_2.png]

Databases in Python:

There are many ways to use Databases in python, like using it through Oracle, MySQL ... etc. But in this tutorial I'll use MySQL to make Databases.

Installing MySQL:

Check this website it's for Python 2.7 Download Link

Make Your First Python Database:

First we must import the sqlite module
Code:
import sqlite3

To create a database insert the next line
Code:
CreateDB = sqlite3.connect('Py_Database.db')

At the above code you can replace the name of the database with this :memory: to make this DB in the memory and delete itself after finishing the program.
it's like a temporary DB.
Code:
CreateDB = sqlite3.connect(':memory:')

By this step you have created a database check the working folder and you will find it Thumbsup.

Creating a Table

Now after creating the database it's the time to add info to the database, first we must create table to save data

To execute commands or to talk with the database we have to set the cursor so we can do this

Code:
curs = CreateDB.cursor()

After defining the curs variable at previous step we enter the following

Code:
curs.execute(''' CREATE TABLE accounts (id INTEGER PRIMARY KEY, account TEXT, pass TEXT, info TEXT) ''')

well, in this step we created table with the name 'accounts' then we defined the information we will add to the table in the DB, first we define something like a serial for the data in the table then we define the other information which is in this case, the account then the password for the account and info, and we followed each definition with the type of it whether it's TEXT or INTEGER or REAL ..etc - in this tutorial i make some basic program to save passwords for my accounts as example Confusedmile:.

Add Info to Table

Now we created the database and the table to add data to it, now we add the data, to add data to the database we should choose what table to add data to, then we add the data.

Code:
curs.execute('''INSERT INTO accounts (account, pass, info) VALUES (?,?,?)''',('email@mailengine.com', 'password', 'other info about the account')
Note: the (?,?,?) like using the parameters in function the ? is the parameter and the followed () with values inside i hope u got it Rolleyes.

The next command is to make the database apply all changes we made here
Code:
createDB.commit()

By this step we are done with creating the database and adding data to it Thumbsup



Reading from Database

To read from the database enter the following, but first we add the following to connect to the database and work with it
Code:
createDB = sqlite3.connect('Py_Database.db')
curs = createDB.cursor()

Then the next line to select which table to deal with
Code:
read = curs.execute('''SELECT * FROM accounts''')

Then the next code to read from the database

Code:
for record in read:
    for line in record:
        print line



Here is a basic example for password keeper to save my passwords with accounts, it's basic and need a lot to be good one but it's as i said ... an example

Link: http://pastebin.com/jBdR01QG

Screenshots:







Thumbsup Yeye This is the end. Thanks for reading my tutorial :-) Yeye Thumbsup

Stay Tuned for the next Tut
Reply
#2
Very nice tutorial for beginners.

I have been getting very good in this programming language.

I hope to improve.
Reply
#3
Nice tutorial, i'll be sure to read it all later on.
Reply
#4
Nice, this is where I will start to learn.
[Image: xypfOL.png]
Reply
#5
Nice looking Tutorials.

I am experienced in this language and I have followed similar Tutorials.
Reply
#6
2 months without any posts?
That cant be true?
Anyone love it ;)?
Reply
#7
Now before I read it.
Thank you.
This seems very well informed.
If I have questions I shall post them:$
Reply
#8
Grin if you got any problems just write to me :-)
What tutorial will you guys see next?
Reply
#9
What do you guys think about my tutorial Big Grin?
Reply
#10
What should my next tutorial be about?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Help Dεlluzion 3 2,006 09-30-2019, 12:59 AM
Last Post: samsmith001
  Python tutorial websites for beginners BigdaddyV 6 2,007 04-08-2012, 11:15 AM
Last Post: Mysterious
  Python Programmming Guides, Tutorials, Web Resources, and E-books Compilation simply_mark 2 1,381 12-03-2011, 12:39 PM
Last Post: Rhynorater
  Python Tutorials Compilation Coding Support 7 2,328 07-28-2011, 01:22 PM
Last Post: Terridax
  Simple Python Python Compiler Canoris 21 9,199 06-01-2011, 06:30 PM
Last Post: Filefinder

Forum Jump:


Users browsing this thread: 1 Guest(s)