Wednesday, 29 November 2017

Installing MySQL and SQLite on Windows 10

MySQL is a free version of SQL, the most commonly used database system on the web.
Downloading it from https://dev.mysql.com/downloads/installer/ , going for the web community installation, developer set-up.
Also ended up installing Microsoft Visual C++
Ran into problems where it expected Python 3.4 to be installed, and did not recognise Python 3.6
Installed Python 3.4 from an old download on another computer
On the Python website, 3.4 was as a GZIP, with the script needed to install Python 3.4 as a .sln file which had to be run by MS Visual C++/Studio. I knew this was installed, but I couldn't work out how to open the file using MS Visual C++ - at this point I decided it wasn't worth it.
Setting up this stuff can be painful.
After staring blankly at MySQL workbench, I went back to Google and checked out alternatives to MySQL. Settled on SQLite3.

Downloaded SQLite from https://sqlite.org/download.html
There is no actual installation - simply placing the downloaded files in a suitable folder on your drive - not C:\Program Files - I found that you need to run Python as Administrator to create in/write to folders in Program Files, and I can't be bothered with that.
So now the files are sitting in C:\sqlite\, with an extra folder at C:\sqlite\db\ and it seems okay.
The module you need to import into Python scripts is sqlite3, which is already built into recent Python versions.
I actually got a lot of help from http://www.sqlitetutorial.net/download-install-sqlite/
Although it talks about getting the GUI version, at the moment I will stick with the command-line version just to keep it as simple as possible.
There was also a first Python script to connect to a database, creating one if it doesn't already exist. I modified only the path to find the db file.
import sqlite3
from sqlite3 import Error


def create_connection(db_file):
    """ create a database connection to a SQLite database """
    try:
        conn = sqlite3.connect(db_file)
        print(sqlite3.version)
        conn.close()
    except Error as e:
        print(e)

if __name__ == '__main__':
    create_connection('C:\\sqlite\\db\\base1.db')

And it seems to work as intended (once I got the path right...).
On the Python command line I get
========= RESTART: C:\Users\pc\Documents\Programming\sqlitetest2.py =========
2.6.0
>>>


while in the relevant folder a new file base1.db has just appeared.


Tuesday, 28 November 2017

After a long break, a binary converter

Despite the lack of activity, I haven't completely given up on this blog. Things are getting busier with Open University modules, so more of my mental energy has gone into academic study rather than self-motivated learning about Python.

However, I have come up with something short and sweet. This is something of a step away from some of the more challenging topics I was considering. Classes, objects, regular expressions and SQLite are subjects I am intending to get onto, but currently I am not really confident about posting stuff that I have only just got my head around.

So here I present a program that convert numbers from decimal to binary:
#!/usr/bin/python3

print ("Binary Converter Program")
num = input("Please enter decimal number to be converted: ")
num = int(num)

comparator = 1
digcount = 0
while comparator <= num:
    digcount += 1
    comparator *= 2

print ('Number of digits is', digcount)
print ('Chosen number is less than', comparator)

binstring = ''
while digcount > 0:
    digcount -= 1
    comparator = 2** digcount
    if num - comparator >= 0:
        binstring = binstring + '1'
        num = num - comparator
    else:
        binstring = binstring + '0'
    
if binstring == '':
    binstring = '0'
print ('Binary string is', binstring)
This gives the outputs:
>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
19
Number of digits is 5
Chosen number is less than 32
Binary string is 10011

>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
187
Number of digits is 8
Chosen number is less than 256
Binary string is 10111011

>>>
RESTART: C:\Users\666\Dropbox\Misc Programming\Python\python3\binaryconverter.py
Binary Converter Program
Please enter decimal number to be converted:
2017
Number of digits is 11
Chosen number is less than 2048
Binary string is 11111100001
>>> 
I admit the  printout of "number of digits" and "chosen number is less than" is diagnostic, and probably not useful for any user.
Similarly, I am not sure if this is the simplest, most elegant or most Pythonic way of doing this task, but  it seems to work so that's good enough for me. If there is a better way, feel free to let me know.
A few notes about how I have done this:

  • The digcount variable is first used to record how many binary digits the converted number will have. It is then used to keep track of which column / digit in the binary printout is being tested to see if it should be a '1' or '0'
  • The comparator variable also serves a dual purpose. Firstly to see if the number being converted is bigger or smaller than a power of 2 (i.e. 2 ** x). Secondly while the number is reduced by having powers of 2 subtracted from it, the comparator is used to see if that gives a '1' or a '0' for that digit/power of 2. 
  • There is a lot of use of the +=, -+ and *= operators, to change a variable by a certain amount. 
  • Finally the outputted binary 'number' is actually a string. It just seemed more convenient that way.