Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
EX:NO: PYTHON & MYSQL CONNECTIVITY DATE: Aim: To demonstrate the Python programs and Python program connectivity with MYSQL database. Introduction Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. Python is a high-level programming language.It is an easy to learn, powerful programming language. It has efficient, high-level data structures and a simple yet effective approach to object oriented programming. Python’s elegant syntax and dynamic typing, together with it's interpreted nature, makes it an ideal language for scripting and rapid application development in many areas and on most platforms. The Creator of this Language is Guido van Rossum. He named this language after the BBC show “Monty Python‘s Flying Circus”. But he doesn’t particularly like snakes that kill animals for food by winding their long bodies around them and crushing them. Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines. Python is free to use, even for commercial products, because of its OSI-approved open source license. Steps to execute python programs: 1. Open the vi editor and write the program 2. Save the program with .py as extension 3. Execute the program using python filename.py Program : CREATE TABLE PROGRAM USING PYTHON #!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","root","","student" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS student ") # Create table as per requirement sql = """CREATE TABLE student ( FIRST_NAME CHAR(20) NOT NULL,LAST_NAME CHAR(20), AGE INT, SEX CHAR(1), cursor.execute(sql) # disconnect from server db.close() Output: Go to terminal: [fosslab@fosslab ~]$ su Password: (admin123) [root@fosslab ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.1.51 Source distribution Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | TESTDB | | college | | employee | | mysql | | sample | | steel | | student | test | | +--------------------+ 9 rows in set (0.00 sec) mysql> use student; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> connect; Connection id: 27 Current database: student mysql> show tables; +-------------------+ | Tables_in_student | +-------------------+ | EMPLOYEE | | courses | | student | +-------------------+ 3 rows in set (0.00 sec)