12-27-2011, 04:05 PM
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
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
To create a database insert the next line
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.
By this step you have created a database check the working folder and you will find it .
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
After defining the curs variable at previous step we enter the following
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 mile:.
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.
Note: the (?,?,?) like using the parameters in function the ? is the parameter and the followed () with values inside i hope u got it .
The next command is to make the database apply all changes we made here
By this step we are done with creating the database and adding data to it
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?
Spoiler (Click to View)
If you know dont read this part
Database is a collection of well formed and organized data that u can use and make good things.
For more Info: Wiki
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 .
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 mile:.
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')
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
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:
Figure-1 (Click to View)
Figure-2 (Click to View)
Figure-3 (Click to View)
This is the end. Thanks for reading my tutorial :-)
Stay Tuned for the next Tut