Posts

Python with mysql codes

Fundamentals to mysql to work with python ======================================================= 1. Login to database by importing connector and using connect statement   from mysql import connector as con mydb = con.connect(host ='localhost', user='root', password='admin')   2.create a cursor to move through mydb cur = mydb.cursor()   3.execute command with execute() function cur.execute() #commands in parantheses   4.display results with functions as fectchall() results = cur.fetchall()   5.loop in through results for data in results:     print(data) ========================================================== In Further sections we will go through some examples and know how the things work with sql ========================================================== 1. Making a connection to sql server:- from mysql import connector as con mydb = con . connect ( host = 'localhost' , user = 'root' , password = &

Rough ideas

  Selecting a particular item column wise from mysql database:- #below syntax will query value from column named "id" SELECT DISTINCT id FROM menu; (; is a delimiter to separate the syntax) Where is used to declare a condition in mysql to narrow the result of the search mysql> SELECT * FROM menu WHERE id='5'; Allows user to sort table by column (name) SELECT * FROM menu GROUP BY name; To show tables in database SHOW TABLES in <database name>; To interact with Database Python follows below mechanism 1. import mysql 2. import connector from mysql or map to a variable in python 3. make connection with database 4.make a cursor  5.then execute commands 6.close the database We will see that we are following this hierarchy through out our examples.. First import python with following command from mysql import connector as con # I have imported 'connector' function from mysql and map to con