Wednesday, August 5, 2015

Python: Create a simple Sqlite Database

This tutorial will teach you how to create a simple SQLite (SQLite) database in python. SQLite is pretty much different to the normal 

Step 1:
Create a file called my_database.py

Step 2: 
Import the necessary items that you will need for this.
Since we are going to use an SQLite database. We need to import it. Also just to add some fun to it, lets add the timestamp to every row that we create. In order to use the time, import it as shown in the second statement.
 import sqlite
 import datetime

Step 3:
Create a connection that will create your database. If the database exists, it will connect to it, if not the database will be created. Also create a cursor. The cursor is provided by Python sqlite3 to perform operations such as executing SQLite statements. When connecting we add the name of the database. In this case 'my_database.sqlite'
 conn = sqlite3.connect('my_database.sqlite')  
 cursor = conn.cursor()  

Step 4:
Now that our script is ready. Lets proceed with creating a table. To make things prettier, lets place it in a method.
 def table_create():  
   cursor.execute("CREATE TABLE IF NOT EXISTS my_table("  
           "id INTEGER PRIMARY KEY AUTOINCREMENT,"  
           "name TEXT,"  
           "description TEXT,"  
           "age INTEGER,"  
           "timestamp TEXT"  
           ")")  

Step 5:
Now our table is ready, lets add some dummy data. Again, making it prettier by adding it in a method.
 def insert_dummy_data():  
   timestamp = str(datetime.datetime.now())  
   cursor.execute("INSERT INTO my_table (name, description, age, timestamp) VALUES('Jonathan', 'Software Engineer', 22,'" + timestamp + "')")  
   cursor.execute("INSERT INTO my_table (name, description, age, timestamp) VALUES('Chethiya', 'Marketing Executing', 22,'" + timestamp + "')")  
   cursor.execute("INSERT INTO my_table (name, description, age, timestamp) VALUES('Charith', 'Business Development Executive', 23,'" + timestamp + "')")  
   conn.commit()  

Step 6:
Everything we need to do has been done except for calling our methods. Lets do that now.
 def main():  
   table_create()  
   insert_dummy_data()
  
 if __name__ == "__main__": main()  

Finally:
Execute this in the command prompt
 python my_database.py  


The database should be created in the same directory as your python script. A SQLite database explorer can be used to view the database. I use the SQLite Manager Add on in Firefox.

Always remember write pretty code, and by pretty i mean neat, fast, efficient code! ;)

See the whole script file here: my_database.py 


Feel free to comment :)
Cheers!

Friday, December 5, 2014

Getting Started with Python - Installing Python on Windows

Python is a language that is very easy to learn. A few lines of code can be used to do so much. This tutorial will pretty much show you how to get started with Python on a computer thats running Windows.


  1. We first start by downloading python. Click here to go to the Python downloads page.
  2. I would recommend that you download Python 3.x as Python 2.x is pretter old and is getting out dated. What is currently available for download is Python 3.4.2 and that is the version that I will be working with.
  3. Once you have downloaded Python. Go to your downloads directory, by default its usually 'C:\Users\<your user name>\Downloads' and find 'python-3.4.2.msi'. Double click this and it should show you a nice installer. Pretty easy!


  • This is pretty much like every other installation you've ever done, mostly clicking next, so if you've done any installations in ever, you'll feel pretty much at home. This pretty much what you will see when you open the installer. Click next.
  • This is where you can specify where you want to install Python. I recommend using the default. If you change the location, make sure you remember where you put it. Click next.
  • Next again!
  • It's going to start installing Python on your computer. You might see a black screen pop in and then dissapear, relax, it's all normal. Once the installation is done you will see some thing like whats shown below. Click Finish and tada, you've successfully installed Python on your computer. 

  • So now that we have it installed, go to the location where you installed it. The default location would be 'C:\Python34'. Look for a file called 'python.exe'. Copy the location of the file, eg: 'C:\Python34'
 
  • If you're seeing that file, then you're on the right track. Now for the last thing you have to do. You have to add the location of the 'python.exe' to your path variable.
    • Right click your 'My Computer' icon and go to 'Properties'
    • Click on 'Advanced System Settings', make sure the 'Advanced' tab is selected and look for 'Environment Variables'
    • Scroll through the list of System Variables and look for a variable called Path or PATH. Click on that variable, and click on edit. Bring the cursor to the end of the text that is already in the 'variable value' field and check and see if there is a semi colon, If there isn't add one and then paste the location of the 'python.exe' file we copied previously. So basically you will be pasting 'C:\Python34'. Make sure you add a semi-colon before you paste the location if there already is text in the 'variable value' field.
    • If there is no Path variable you can create one called 'Path' and add the location to the variable value.


  • Congratulations! You have now successfully finished installing Python. To see the version through the command line open the command prompt by hitting the win + R keys to gether and then in the resulting window type cmd and press enter. Now type Python -V. If you did everything right you would see the version of Python that has been installed.




If you like what you see leave a comment and share for everyone! ;)